Forum: Ruby on Rails check if controller action??

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.
7c86cccf24556c4ebe08085345c53d19?d=identicon&s=25 Roger Muthton (rogermuth)
on 2009-02-18 13:38
Hi Chaps,

How can I check if the page is using a controllers action. for example i
got a helper method that needs to know if a certain controller action is
run on the page or not so as to display different content for it.

Thought it was just

code = "xyz" if Controllername.action?

Cheers,

Rog
69112da010aa0192e775a4236cd9af3a?d=identicon&s=25 Xuan (Guest)
on 2009-02-18 15:56
(Received via mailing list)
On Feb 18, 1:38 pm, Roger Muthton <rails-mailing-l...@andreas-s.net>
wrote:
> Cheers,
>
> Rog
> --
> Posted viahttp://www.ruby-forum.com/.

If I correctly understood what you need, you may try using:
> Controller.action_methods.include?params[:action]
Where params[:action] will contain the slice of the url corresponding
to the action in the last request
91a72b0ad3e43e3ef3a31816667ec694?d=identicon&s=25 bill walton (Guest)
on 2009-02-18 17:02
(Received via mailing list)
Hi Roger,

On Wed, 2009-02-18 at 13:38 +0100, Roger Muthton wrote:
> Hi Chaps,
>
> How can I check if the page is using a controllers action. for example i
> got a helper method that needs to know if a certain controller action is
> run on the page or not so as to display different content for it.
>
> Thought it was just
>
> code = "xyz" if Controllername.action?

Your views have access to the controller and method that rendered them
via controller.controller_name and controller.action_name

e.g.,
<% if controller.controller_name == 'posts' %>
  <% if controller.action_name == 'create' %>
    <%= do whatever %>
  <% end %>
<% end %>

HTH,
Bill
This topic is locked and can not be replied to.