I have a Rails 5 application that will have dynamic content in the header based on the current route. I realize that I can get the route and compare it with something like if(current_page?('/home'))
but is this really the best way?
Asked
Active
Viewed 278 times
0

Drakken Saer
- 859
- 2
- 10
- 29
3 Answers
1
I think using params[:controller]
, params[:action]
or current_page? would be better

user3033467
- 1,078
- 15
- 24
1
You can use named routes with current_page?:
if(current_page?(posts_path))
and
if(current_page?(post_path(1))
In addition, you can also check the the current path by using request
if you're in root path
request.url
#=> "http://localhost:3000"
request.path
#=> "/"

mrvncaragay
- 1,240
- 1
- 8
- 15
1
Think of initializing your dynamic content in controller's actions. It is the common solution for such things.
For example:
html:
<title><%= @title %></title>
controller:
def index
...
@title = 'Hello world'
...
end
Also, you can initialize dynamic content in Rails views using content_for
helper method. There is a similar question about this Rails: How to change the title of a page?
If you are sure that you have to set this content in a different place and based on the current route, I advise you to use params[:controller]
and params[:action]
parameters, although your solution is not bad too.
-
I am in agreement with your solution, I feel like the controller should be concerned with something like this, rather than the view. Thank you. – Drakken Saer Aug 03 '16 at 23:26