ruby-on-rails - Rails : on redirect to same page form submitted from, 闪存消息未显示

标签 ruby-on-rails

在提交表单到 sexes_controller 中的“创建”操作时,我已经更改了一些内容以实际重定向回提交表单的页面 View /结果/索引,但现在即使我在做

<%= flash[:notice] %>

在 views/results/index 的底部(即提交表单的页面),这就是我认为您应该这样做的方式。

是不是因为正在发生某种缓存,所以没有显示 Flash 消息?知道如何解决这个问题吗?

更新

认为它可能更复杂,我尝试在结果 Controller 的索引操作中检索 flash 消息

@flashbash = Sex.find(params[:id])

然后回到views/results/index

<%= if @flashbash flash[:notice] %>  (I think this code is wonky)

请注意,我试过了,但没有用。它说,Couldn't find Sex without an ID

有什么办法可以解决这个问题吗?

最佳答案

通常,Flash 在应用程序的布局文件中呈现。这避免了在每个可能有闪现消息的 View 中重复输出 <%= flash[:notice] %>。

至于它为什么不显示,请检查您是否将 flash[:notice] 变量设置为要显示的内容。 Controller 中创建操作的示例可能如下所示:

# app/controllers/sex_controller.rb
def create
  @sex = Sex.new(params[:sex])
  if @sex.save
    flash[:notice] = "Saved successfully"
    redirect_to @sex # This redirects to the show action, where the flash will be displayed
  else
    flash[:error] = "There were errors..."
    render :action => :new # This displays the new form again
  end
end

# app/layouts/application.html.erb
<html>
  ...
  <%= flash[:notice] %>
  <%= flash[:error] %>
  <%= yield %>
  ...
</html>

更多信息请点击此处:http://guides.rubyonrails.org/action_controller_overview.html#the-flash

关于ruby-on-rails - Rails : on redirect to same page form submitted from, 闪存消息未显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8370449/

相关文章:

ruby-on-rails - 从 Rails 应用程序(Word、PDF、Excel 等)中搜索附件

ruby-on-rails - 我如何将数据从 javascript 的 canvas.toDataURL 保存到 ruby​​ 中的图像文件?

ruby-on-rails - 提高 ActiveRecord 查询的性能

javascript - ActionController::RoutingError(没有路由匹配 [GET] "/assets/javascripts

javascript - 如何更新 Rails 中 Ajax 删除的记录?

javascript - Rails-找出JavaScript配置

javascript - 解析 index.html.erb 文件中脚本标签内的 ruby​​ 散列

ruby-on-rails - 将 block 传递给动态创建的方法

ruby-on-rails - 在 rails 中缓存 http 调用结果

ruby-on-rails - 动态 body ID