Current_page?()

Can someone give me a working example of current_page?(), because I’ve
been trying to get this to work for a while now. I’ve been trying this:

if current_page?(".*")
@test = “asd”
end

but get a “undefined method `current_page?’” when I put it in my
controller or application.rb. If I put it in my application_helper.rb,
the page renders fine, but the test variable still won’t show.

Any ideas?

Thanks in advance,

  • Seb

Sebastian –

current_page is a method of the UrlHelper module. Methods in Rails’
inbuilt helper modules are available within views, but not in models
and not in controllers. For good reason, because helpers are really
just a way to keep representation-related code out of your templates.

So if you’re using current_page? in a view or in another helper,
you’re on the right track, in terms of “doing the right thing”.

If I put it in my application_helper.rb,
the page renders fine, but the test variable still won’t show.

Really can’t help you here without more context. Whats the relevant
code in your view and your application_helper.rb?

cheers
Gerret

My issue was that I wanted to have a layout file for all different
controllers, but have a dynamic menu so that I could have a different
menu icon in the menu depending on which page was displayed. For that, I
wanted to check which the current page was by using the current_page?()
method.

However, I managed to work around this by using a page title variable I
set for all different controllers, and it worked flawlessly. Now I just
check and see if the page title matches the link title in the menu, and
that seems to work. :slight_smile:

In your layout, you can retrieve the current controller action using:
<%= controller.action_name %>

And you can retrieve the current controller’s name using:
<%= controller.controller_name %>

Dominique
This might work, yes. I’ll try it and see what happens! :wink:

Hmmm… maybe a better way of doing it is this…

In your layout, you can retrieve the current controller action using:
<%= controller.action_name %>

And you can retrieve the current controller’s name using:
<%= controller.controller_name %>

IMHO this might be better than checking the page title, since this will
enable you to decouple your menu logic from the page title.

Hope this helps!

Dominique

Sebastian Conrad wrote:

My issue was that I wanted to have a layout file for all different
controllers, but have a dynamic menu so that I could have a different
menu icon in the menu depending on which page was displayed. For that, I
wanted to check which the current page was by using the current_page?()
method.

However, I managed to work around this by using a page title variable I
set for all different controllers, and it worked flawlessly. Now I just
check and see if the page title matches the link title in the menu, and
that seems to work. :slight_smile: