Question

I need to place all my flash notices below header, except of one page.

Only on one page I should place it differently.

EDIT

I added in my acton, where I want to use another layout this line:

  render :layout => 'show'

and now, when I'm going to show page it gives me:

  undefined method `url' for nil:NilClass

but without rendering THIS layout - no errors.

EDIT

Is it possible in Rails ?

Was it helpful?

Solution

Off the top of my head, you could just do a check on the current controller#action in the view to see if our flash messages need to be moved.

Rails passes these params to every view (ex: if we were in the show action of UsersController):

params[:controller] #=> users
params[:action]     #=> show

We could add a helper to simplify our condition in the view:

# file: app/helpers/application_helper.rb
def move_flash_messages?
  params[:controller] == "users" && params[:action] == "show"
end

And then do this simple check in our layout:

# file: app/views/layouts/application.html.haml
- unless move_flash_messages?
  = flash_messages
...
- if move_flash_messages?
  = flash_messages

OTHER TIPS

This is very easy to show flash message anywhere

  <% flash.each do |name, msg| %>
    <%= content_tag :div, msg, class: "flash notice" , id:"flash_notice" %>
  <% end %>

here is css for flash message

div.flash.notice {
  background: url(../images/true.png) 8px 5px no-repeat;
  background-color: #dfffdf;
  border-color: #9fcf9f;
  color: #005f00;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top