I need to know what controller and action the user is currently in to do various things in my layouts/application.rhtml file. Isn''t there is a better way than: <% if params[:controller] == "whatever" && params[:action] == "whatever" %>some html<% end %> I know in the controllers you can just use controller_name, but that is not provided in the views. Thanks for your help. Thank You, Ben Johnson E: bjohnson@contuitive.com -------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060722/00d23b12/attachment.html
Why not just assign controller_name to a @variable to use in your views? Ben Johnson wrote:> I need to know what controller and action the user is currently in > to do various things in my layouts/application.rhtml file. Isn''t > there is a better way than: > > <% if params[:controller] == "whatever" && params[:action] => "whatever" %>some html<% end %> > > I know in the controllers you can just use controller_name, but that > is not provided in the views. > > Thanks for your help. > > Thank You, > Ben Johnson > E: bjohnson@contuitive.com-- Posted via http://www.ruby-forum.com/.
Popular question today. I have the following in one of my layout head sections <%= "admin::#{controller.controller_name}::#{controller.action_name}" %> That might get you started. Peter On 7/22/06, Ben Johnson <bjohnson@contuitive.com> wrote:> > I need to know what controller and action the user is currently in to do > various things in my layouts/application.rhtml file. Isn''t there is a better > way than: > > <% if params[:controller] == "whatever" && params[:action] == "whatever" > %>some html<% end %> > > I know in the controllers you can just use controller_name, but that is not > provided in the views. > > Thanks for your help. > > > Thank You, > Ben Johnson > E: bjohnson@contuitive.com > > > > > > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails > > >
A controller''s purpose in life is to determine what view to render. If you find yourself conditionally rendering views _within_ a view, it''s probably time to look at your controller again. - Rabbit PS: But to answer your question, the "proper" way to access controller and action names would be through params. (e.g. params[:controller]) --- On 7/22/06, Peter Michaux <petermichaux@gmail.com> wrote:> > Popular question today. > > I have the following in one of my layout head sections > > <%= "admin::#{controller.controller_name}::#{controller.action_name}" %> > > That might get you started. > > Peter > > On 7/22/06, Ben Johnson <bjohnson@contuitive.com> wrote: > > > > I need to know what controller and action the user is currently in to > do > > various things in my layouts/application.rhtml file. Isn''t there is a > better > > way than: > > > > <% if params[:controller] == "whatever" && params[:action] == "whatever" > > %>some html<% end %> > > > > I know in the controllers you can just use controller_name, but that is > not > > provided in the views. > > > > Thanks for your help. > > > > > > Thank You, > > Ben Johnson > > E: bjohnson@contuitive.com > > > > > > > > > > > > _______________________________________________ > > Rails mailing list > > Rails@lists.rubyonrails.org > > http://lists.rubyonrails.org/mailman/listinfo/rails > > > > > > > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails >-------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060724/e1cf7422/attachment.html
On Monday, July 24, 2006, at 4:11 PM, Rabbit wrote:>A controller''s purpose in life is to determine what view to render. If you >find yourself conditionally rendering views _within_ a view, it''s probably >time to look at your controller again. > >- Rabbit > >PS: > >But to answer your question, the "proper" way to access controller and >action names would be through params. (e.g. params[:controller]) > >--- > >On 7/22/06, Peter Michaux <petermichaux@gmail.com> wrote: >> >> Popular question today. >> >> I have the following in one of my layout head sections >> >> <%= "admin::#{controller.controller_name}::#{controller.action_name}" %> >> >> That might get you started. >> >> Peter >> >> On 7/22/06, Ben Johnson <bjohnson@contuitive.com> wrote: >> > >> > I need to know what controller and action the user is currently in to >> do >> > various things in my layouts/application.rhtml file. Isn''t there is a >> better >> > way than: >> > >> > <% if params[:controller] == "whatever" && params[:action] =>>"whatever" >> > %>some html<% end %> >> > >> > I know in the controllers you can just use controller_name, but that is >> not >> > provided in the views. >> > >> > Thanks for your help. >> > >> > >> > Thank You, >> > Ben Johnson >> > E: bjohnson@contuitive.com >> > >> > >> > >> > >> > >> > _______________________________________________ >> > Rails mailing list >> > Rails@lists.rubyonrails.org >> > http://lists.rubyonrails.org/mailman/listinfo/rails >> > >> > >> > >> _______________________________________________ >> Rails mailing list >> Rails@lists.rubyonrails.org >> http://lists.rubyonrails.org/mailman/listinfo/rails >> > > >_______________________________________________ >Rails mailing list >Rails@lists.rubyonrails.org >http://lists.rubyonrails.org/mailman/listinfo/rails >There are plenty of good reasons to want to know what controller you are in. Particularly if you have some meta magic going on in a helper that spans multiple controllers. controller.controller_name and controller.action_name are the correct functions to call. _Kevin www.sciwerks.com -- Posted with http://DevLists.com. Sign up and save your mailbox.