Rails current URL helper

Sorry for such a simple question, but I have not been able to solve it myself after a few hours since my RoR knowledge is mostly lacking. In the Rails application I'm working with, a navigation assistant was used to highlight the active menu:

  def nav_link(link_text, link_path, ico_path)
    class_name = current_page?(link_path) ? 'active' : nil

    content_tag :li do
      link_to(link_path, class: class_name) do
        image_tag("icons/#{ico_path}.svg") + content_tag(:span, link_text)
      end
    end
  end

Conditions have changed, and is current_page?no longer a viable option since routing is now handled on the interface. Is there a way to achieve the same functionality by extracting, for example, the current URL and checking it for link_path? I tried many things with the help of various helpers, such as request.original_url, but to no avail.

+4
source share
3 answers

request.original_url .

URL-

http://apidock.com/rails/ActionDispatch/Request/original_url

.

request.host + request.full_path

,

url_for(:only_path => false);

+5

active_link_to gem:

def nav_link(link_text, link_path, ico_path)
    content_tag :li do
      active_link_to link_path do
        image_tag("icons/#{ico_path}.svg") + content_tag(:span, link_text)
      end
    end
end

current_page?, active_link_to , , .

current_page? , , .

-

, current_page? .., , , . , , ...

.

Of course, even if you use something like Angular with Rails, will you need to set up routes for your application?

+1
source

Source: https://habr.com/ru/post/1623695/


All Articles