Search code examples
ruby-on-railsrubyalertnotice

Ruby on Rails - Can I choose where a specific notice will be shown


In my application I am using Bootstrap flash in my header to show all notices/alerts.

Here is my code:

.col-md-8.col-md-offset-2
  = bootstrap_flash
.clearfix
.row
  = yield

I have a vote controller that I would like to show its alert in completely different place, next to the vote button itself.

How can I "choose" where a specific notice from specific controller will be shown?


Solution

  • Here is a HAML way. Just copying the @David answer with haml.

    If you change your layout to:

    .col-md-8.col-md-offset-2
      = bootstrap_flash unless content_for?(:custom_flash)
    

    And in the view for your form you put:

    - content_for(:custom_flash) do
      = bootstrap_flash
    

    And by the button you do:

    = yield(:custom_flash)
    

    This way your normal flashes will be shown unless you define a content_for(:custom_flash)