Search code examples
rubyruby-on-rails-3renderpartial

Conditionally render partials in application layout


In a Rails 3 application layout i include a partial for flash messages. While this is ok in most cases, there is a view where I would need flash messages appear in another place; is there a way in layouts/application.html.erb to say something like

<%= render 'layout/messages' unless somecondition %>

where somecondition is something able to detect that I am in 'myview/index'?


Solution

  • Sure is, use params[:controller] and params[:action] actually you should use controller_name and action_name per the Rails documentation

    <%= render 'layout/messages' 
      if controller_name == 'myview' && action_name == 'index' %>