Flash notice is not working. I am using rails 5.1. My code is like this:
def message
redirect_to users_path, notice: "Message"
end
<% if flash.present? %>
<% flash.each do |k, v| %>
<p class="abc" id="a"><%= v %></p>
<% end %>
<% else %>
<p class="a" id="b"></p>
<% end %>
Flash Message is coming few times and few times it's not coming, It's going in else block.
So, for this I have fixed by using flash.keep in users index controller. But Now in every page whenever I am redirecting,that flash message is coming.
You can try as:
def message
flash[:notice] = 'Message'
redirect_to users_path
end
And in your view:
<% if flash[:notice].present? %>
<% flash.each do |k, v| %>
<p class="abc" id="a"><%= v %></p>
<% end %>
<% else %>
<p class="a" id="b"></p>
<% end %>
Hope it can solve your problem.
I think the flash hash is always present. You can check if it's empty instead https://api.rubyonrails.org/classes/ActionDispatch/Flash/FlashHash.html#method-i-empty-3F
<% if flash.empty? %>
<p class="a" id="b"></p>
<% else %>
<% flash.each do |k, v| %>
<p class="abc" id="a"><%= v %></p>
<% end %>
<% end %>
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.