重定向或渲染后不显示Flash消息

使用导轨4和ruby2

无法显示来自控制器的Flash消息。 我的方法看起来像这样:

def create @salary_report = SalaryReport.create(salary_report_params) if @salary_report.save redirect_to @salary_report flash[:notice] = "Lönerapporten sparades korrekt!" puts "salary report saved #{flash[:notice]}" else render :new, notice: "Något gick fel när lönerapporten skulle sparas!" end end 

正如您所看到的,我添加了一个puts语句,打印出flash通知只是为了certificate重定向后生成了闪存通知。

创建薪水报告后,日志如下所示:

 Redirected to http://localhost:3000/salary_reports/20 salary report saved Lönerapporten sparades korrekt! Completed 302 Found in 25ms (ActiveRecord: 9.7ms) 

在显示查看日志后:

 Started GET "/salary_reports/22" for 127.0.0.1 at 2013-07-24 16:08:42 +0200 Processing by SalaryReportController#show as HTML Parameters: {"id"=>"22"} SalaryReport Load (0.5ms) SELECT "salary_reports".* FROM "salary_reports" WHERE "salary_reports"."id" = ? LIMIT 1 [["id", "22"]] Document Lo ad (0.3ms) SELECT "documents".* FROM "documents" WHERE "documents"."salary_report_id" = ? [["salary_report_id", 22]] Rendered salary_report/show.html.erb within layouts/application (6.1ms) User Load (0.3ms) SELECT "users".* FROM "users" WHERE "users"."id" = 3 ORDER BY "users"."id" ASC LIMIT 1 Completed 200 OK in 62ms (Views: 58.0ms | ActiveRecord: 1.1ms) 

在视图中,我用以下内容显示消息:

   <div class="alert alert-"> ×  "flash_#{name}" %> 

我已经尝试了各种不同的方法来编写控制器方法,但似乎没有任何帮助。 很不确定问题可能是什么。

您正在重定向后设置flash[:notice] 。 尝试切换这些调用的顺序,即先设置flash消息然后重定向第二个:

 def create @salary_report = SalaryReport.create(salary_report_params) if @salary_report.save flash[:notice] = "Lönerapporten sparades korrekt!" puts "salary report saved #{flash[:notice]}" redirect_to @salary_report else render :new, notice: "Något gick fel när lönerapporten skulle sparas!" end end 

尝试在重定向子句中定义flash消息:

 def create @salary_report = SalaryReport.create(salary_report_params) if @salary_report.save redirect_to @salary_report, notice: "Lönerapporten sparades korrekt!" else render :new, notice: "Något gick fel när lönerapporten skulle sparas!" end end 

尝试使用flash.keep。

你放在闪光灯中的任何东西都会暴露在下一个动作中然后被清除。

在这种情况下,flash传递给2个动作,另一个控制器和一个视图。

 def create @salary_report = SalaryReport.create(salary_report_params) if @salary_report.save flash.keep[:notice] = "Lönerapporten sparades korrekt!" puts "salary report saved #{flash[:notice]}" redirect_to @salary_report else render :new, notice: "Något gick fel när lönerapporten skulle sparas!" end end 

首先,不要单独定义Flash消息,而是将其与重定向内联,就像您在下面使用渲染线一样。 这更像是一种风格变化,而不是任何主要function:

 def create @salary_report = SalaryReport.create(salary_report_params) if @salary_report.save redirect_to @salary_report, notice: "Lönerapporten sparades korrekt!" else render :new, notice: "Något gick fel när lönerapporten skulle sparas!" end end 

至于它不显示的原因,这很可能是您的Flash消息视图代码的问题。 这是因为,正如您测试的那样,正在正确设置闪存哈希,它只是没有显示。

尝试使用以下更简单的版本暂时替换您的Flash消息代码。 确保它位于top.html附近的application.html.erb中,但这又是造型:

 <% flash.each do |name, msg| %> <%= content_tag :p, msg if msg.is_a?(String) %> <% end %> 

由于设计gem,我遇到了同样的问题,我找到了一个带有cookie的解决方案。

您必须在重定向之前设置一个带有值的cookie,然后控制器测试是否有cookie。 如果是,它会测试它的值是什么并显示flash消息。

看看那里有一个完整的解释: flash [:notice]不能使用after_sign_out_path_for – (设计)

希望它可以帮到你。