重定向时闪烁消息

时间:2020-08-27 00:13:58

标签: ruby-on-rails

我在get "*path", to: "pages#home"的底部添加了routes.rb,以捕获所有不存在的路由并重定向到home。我想显示类似flash[:warning] = "This pages does not exists."的内容,但我不知道该在哪里做。有什么建议吗?

3 个答案:

答案 0 :(得分:0)

您需要在home页中添加。

  <% flash.each do |type, msg| %>
    <div>
      <%= msg %>
    </div>
  <% end %>

答案 1 :(得分:0)

对我有用

def home
  return if params[:path].blank?
  
  flash[:alert] = 'This pages does not exists.' 
  redirect_to root_path
end

答案 2 :(得分:0)

将警报添加为redirect_to方法的arg

redirect_to root_path, alert: 'This pages does not exists.' 

如果您想为Flash哈希尝试自定义密钥,也可以这样操作

redirect_to root_path, flash: { alert: 'This pages does not exists.' }