ruby-on-rails – 重定向或渲染后不显示flash消息
发布时间:2020-12-17 03:16:32 所属栏目:百科 来源:网络整理
导读:使用导轨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!
使用导轨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通知只是为了证明重定向后生成了flash通知. 创建薪水报告后,日志如下所示: 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) 在视图中,我用以下内容显示消息: <% flash.each do |name,msg| %> <% if msg.is_a?(String) %> <div class="alert alert-<%= name == :notice ? "success" : "error" %>"> <a class="close" data-dismiss="alert">×</a> <%= content_tag :div,msg,:id => "flash_#{name}" %> </div> <% end %> <% end %> 我已经尝试了各种不同的方法来编写控制器方法,但似乎没有任何帮助.很不确定问题可能是什么. 解决方法
重定向后,您正在设置闪光灯[: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 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |