For example, I have a footer
<%= link_to 'Tweets', tweets_path %>
<%= link_to 'Blogs', blogs_path %>
In the Tweets Index Page, I want to hide the <%= link_to 'Tweets', tweets_path %>
. and show something else. How do I know what resource
the user is currently on?
Specifically, I want to
resources = ['Tweet', 'Blog'] # get the model names, and there maybe something more to be added later
resources.each do |resource|
if controller.controller_name = resource && controller.method_name = 'index'
link_to new_resource_path # for example, link_to new_tweet_path
else
link_to resource_path # for example, link_to tweets_path
end
end
The rough Idea is above. But in controller.controller_name
and link_to
method, I do not know the details of writing it.
I find controller.controller_name
from Can I get the name of the current controller in the view?
What would be a good way to do this?
UPDATE:
def footer_helper
resources = ['tweet', 'blog'] # and perhaps something more
resources.each do |resource|
if current_page?(controller: resource.pluralize, action: 'index')
link_to "New #{resource.humanize}", {controller: resource.pluralize, action: 'new'}
else
link_to "#{resource.pluralize.humanize}", {controller: resource.pluralize, action: 'index'}
end
end
end
end
Now I've made it into a helper like above. But I find the .pluralize
and .humanize
irritating, is there any way to get rid of them?
Also, how can I use it in views? when I use <%= footer_helper %>
, it shows ["tweet", "blog"]
. It does not return properly.