flash[:alert] not working, but flash[:notice] displays the message on redirect

北慕城南 提交于 2020-01-14 04:59:04

问题


I am using rails_admin gem and the configuration is as follows

config.authorize_with do
  if current_user.nil? || current_user.role != 'admin'
    redirect_to main_app.root_path
    flash[:alert] = "Sorry you are not authorized!"
  end
end

When I use flash[:notice], I can see the message on the root_path, but if I change it to flash[:alert] it does not display, any ideas why and what would be the solution?

I want to use :alert only, since changing the notice color will result it displaying red font for all other notices.


回答1:


It is likely that you're not outputting the flash[:alert] hash out onto your page.

Look in your views for where you output flash[:notice] (probably in app/views/layouts/application.html.erb (or similar) and copy the line for your notice to alert. It should look something like this:

<% if flash[:alert] %>
      <div id="alert">
         <%= flash[:alert] %> 
      </div>
<% end %>

If something like that doesn't exist (or you can't find it), add it to your view (or layout)



来源:https://stackoverflow.com/questions/32690339/flashalert-not-working-but-flashnotice-displays-the-message-on-redirect

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!