【问题标题】:Flash messages not working in Rails 3.2.2在 Rails 3.2.2 中 Flash 消息不起作用
【发布时间】:2012-03-06 13:47:13
【问题描述】:

这让我发疯了。我有一个新的Rails 3.2.2 资产管理应用程序。

这是我的控制器:

class AssetsController < ApplicationController
  respond_to :html, :json, :xml

  def index
    respond_with(@assets = Asset.all)
  end

  def show
    @asset = Asset.find(params[:id])
    respond_with @asset
  end

  def new
    @asset = Asset.new
    respond_with @asset
  end

  def create
    @asset = Asset.new(params[:asset])
    if @asset.save
      flash[:notice] = "Asset created successfully"
    end
    respond_with @asset
  end

  def edit
    @asset = Asset.find(params[:id])
    respond_with @asset
  end

  def update
    @asset = Asset.find(params[:id])
    if @asset.update_attributes(params[:asset])
      flash[:notice] = "Asset updated successfully"
    end
    respond_with @asset
  end

  def destroy
    # tbd...
  end

end

这是我application.html.erb的一部分

<% flash.each do |name, msg| %>
    <div class="alert alert-<%= name == :notice ? "success" : "error" %>">
        <%= msg %>
    </div>
<% end %>

但是,闪存始终是空的。即使在资产模型成功更新之后。

当我调试 flash 对象时,这是我得到的:

--- !ruby/object:ActionDispatch::Flash::FlashHash
used: !ruby/object:Set
  hash: {}
closed: false
flashes: {}
now: 

我做错了什么?

【问题讨论】:

    标签: ruby-on-rails-3 erb


    【解决方案1】:

    您的代码对我来说看起来不错。 Rails 上的这个bug report 看起来与您的非常相似,因此是否值得更新到最新版本的 Rails? (据报道,Bug 在过去 24 小时内刚刚修复)

    【讨论】:

    • 好吧,这搞砸了。我刚刚创建了另一个资产(位置),并且 Flash 消息可以使用它!但不是“资产”。我升级到 Rails 3.2.2。做了一个捆绑更新。并将两个控制器更改为相同(好吧,除了资产与位置)。为什么它适用于地点而不是资产?呃。
    • 我猜'assets' 可能是 Rails 3.2 中的保留字。除了这个帖子,我找不到任何证据支持这一点:stackoverflow.com/questions/8186183/routing-issue-with-rails-3
    猜你喜欢
    • 2017-03-28
    • 1970-01-01
    • 2016-08-17
    • 1970-01-01
    • 2018-05-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多