flash [:notice]在redirect_to

时间:2015-07-02 20:33:50

标签: ruby-on-rails ruby flash-message

只要我在rails 2.2.2中使用flash[:notice]

redirect_to就会被删除 这是一个超级简单的ruby应用程序,因此不应该很难找到正在发生的事情(尽管我显然找不到它......)

我的routes.rb看起来像这样

resources :happenings
root to: "happenings#index"
post "/happenings/save"

我的HappeningsController看起来像这样

def index
   @happenings = Happening.all
end

def save
   flash[:notice] = "saved"
   redirect_to root_path
end

现在,如果我正在调试并将puts flash[:notice]添加到保存操作,它将打印“已保存”到我的终端 如果我在索引操作中添加`puts flash [:notice],它会在终端上打印一个空行。因此,当索引操作执行时,它显然已被删除。

有什么想法吗?非常感谢?

编辑,这里有更多信息。 save动作由表单

调用
<form method="post" action="/happenings/save">

以下是我提交表单时的服务器输出(并请求保存操作)

Started POST "/happenings/save" for 127.0.0.1 at 2015-07-02 23:16:31 +0200
Processing by HappeningsController#save as HTML
  Parameters: {"happening_name"=>"Test happening", "happening_time"=>"2015-07-24T13:15"}
Can't verify CSRF token authenticity
Redirected to http://localhost:3000/
Completed 302 Found in 1ms (ActiveRecord: 0.0ms)


Started GET "/" for 127.0.0.1 at 2015-07-02 23:16:32 +0200
Processing by HappeningsController#index as HTML
  Rendered happenings/index.html.erb within layouts/application (0.1ms)
Completed 200 OK in 32ms (Views: 28.4ms | ActiveRecord: 0.0ms)

3 个答案:

答案 0 :(得分:1)

https://stackoverflow.com/a/12340941/2011580 如果您在重定向到另一条路径后遇到问题,请使用此功能。

flash.keep

在重定向之前,在您的方法中

答案 1 :(得分:0)

您可能想要这样做:

Settings file: Use default maven settings
Global settings file: Use default maven global settings

然后放置

def save
  redirect_to root_path
  flash[:notice] = "saved"
end

index.html.erb中的任何位置。

答案 2 :(得分:0)

您能否确保您没有从应用程序控制器重新设置任何Cookie?因为flash消息存储在cookie中。你也可以尝试

flash.now[:notice] = '....'

在重定向之前,您可以使用:

flash.keep(:notice)

来源:http://guides.rubyonrails.org/action_controller_overview.html

小费:

  

您可以安装名为pry的宝石。您可以在视图文件中进行设置   如果需要,您可以在服务器中获取交互式shell   服务器为该页面提供服务。在那里,您可以测试所有动态值   载体。你可以理解它们到底发生了什么。