Forum: Ruby on Rails current_page?()

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Sebastian C. (Guest)
on 2006-01-29 02:28
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
Gerret A. (Guest)
on 2006-01-29 15:29
(Received via mailing list)
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
Sebastian C. (Guest)
on 2006-01-29 17:29
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. :)
Dominique P. (Guest)
on 2006-01-30 10:54
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. :)
Sebastian C. (Guest)
on 2006-01-30 11:05
> 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! ;)
This topic is locked and can not be replied to.