ruby-on-rails-3 - 闪光[:notice] doesn't work with redirect_to

标签 ruby-on-rails-3

我正在从 Controller 状态使用:

  flash[:notice] = 'message'
  redirect_to(controller: 'item', action: 'list')

我不知道为什么通知没有显示。

我尝试并检查了很多事情:

  1. flash.keep
  2. flash.keep[:notice] = 'message'
  3. flash[:notice]render 配合使用效果良好
  4. redirect_to(controller: 'item', action: 'list', notice: 'message')
  5. flash.now[:notice] = "Hello world"
  6. flash.now['foo'] = "Hello world"<%= flash['foo'] %>在 View 中
  7. 布局中有一个<%= flash[:notice] %>

我将以下代码放入布局中。当我的 Controller 方法有一个同名的 View 时,flash[:notice] 工作正常。当我尝试访问另一个没有 View 的 Controller 时,就会出现问题。

<% if !flash[:notice].blank? %>
    <div class="notice">
        <%= flash[:notice] %>
    </div>
<% end %>

<% if !flash[:alert].blank? %>
    <div class="alert">
        <%= flash[:alert] %>
    </div>
<% end %>   

有人可以帮忙吗?

信息:

  • ruby (2.0.0)
  • 导轨 (3.2.13)

最佳答案

导轨:http://guides.rubyonrails.org/action_controller_overview.html#the-flash

这应该工作得很好:

flash[:notice] = "My message"
redirect_to root_url

或者:

redirect_to root_url, notice: "Hello world"

但是,您也可能忘记在 View 中呈现通知。因此,您不会看到任何通知。

例如,您应该看到这样的内容:

<% if flash[:notice] %>
  <p class="notice"><%= flash[:notice] %></p>
<% end %>

关于ruby-on-rails-3 - 闪光[:notice] doesn't work with redirect_to,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15621364/

相关文章:

ruby-on-rails - 如何覆盖 Rails 3 中的模型验证消息

ruby - 使用自定义主 ID 在 rails3 中进行路由

ruby-on-rails - Rails 协会 - 至少有一个

ruby-on-rails - rails : How to use "joins" in sql query for a has_one association

ruby-on-rails - Rails link_to 或 button_to 发布带有参数的请求

ruby-on-rails - rails 3 : How to implement multiple checkboxes in a form?

ruby-on-rails-3 - Wicked_PDF 缺少布局

ruby-on-rails-3 - 在不同的应用程序中使用 Heroku 和 Resque

ruby - Rails 3 + Devise + Rspec - Controller 测试中的未定义方法错误

ruby-on-rails - 可以预期这些参数在任何Rails应用程序中总是会提高500吗?