简体   繁体   中英

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?

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)

(I do not know this fancy HAML-thingy ;) )

If you change your layout to:

<div class="col-md-8 col-md-offset-2">
  <% unless content_for?(:custom_flash) do %>
    <%= bootstrap_flash %>
  <% end %>
</div>

And in the view for your form you put:

<% content_for(:custom_flash) do %>
  <%= bootstrap_flash %>
<% end %>

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) .

I hope you can solve the .erb , or perhaps someone can help me to do it in HAML ;)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM