0

我做了一个关于 paper_trail 的简单项目,但是我遇到了一个问题。当我完全创建或更新时,视图中显示了两条消息:

Product was successfully created. undo

Product was successfully created. <a data-method="post" href="/versions/148/revert" rel="nofollow">undo</a>

这是我的控制器文件:

class ProductsController < ApplicationController
  before_action :set_product, only: [:show, :edit, :update, :destroy]

  # GET /products
  # GET /products.json
  def index
    @products = Product.order(:name)
    respond_to do |format|
      format.html
      format.csv { send_data @products.to_csv }
    end
  end

  # GET /products/1
  # GET /products/1.json
  def show
  end

  # GET /products/new
  def new
    @product = Product.new
  end

  # GET /products/1/edit
  def edit
  end

  # POST /products
  # POST /products.json
  def create
    @product = Product.new(product_params)

    respond_to do |format|
      if @product.save
        format.html { redirect_to @product, notice: "Product was successfully created. #{undo_link}" }
        format.json { render action: 'show', status: :created, location: @product }
      else
        format.html { render action: 'new' }
        format.json { render json: @product.errors, status: :unprocessable_entity }
      end
    end
  end

  # PATCH/PUT /products/1
  # PATCH/PUT /products/1.json
  def update
    respond_to do |format|
      if @product.update(product_params)
        format.html { redirect_to product_url, notice: 'Product was successfully updated.' "#{undo_link}" }
        format.json { head :no_content }
      else
        format.html { render action: 'edit' }
        format.json { render json: @product.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /products/1
  # DELETE /products/1.json
  def destroy
    @product.destroy
    respond_to do |format|
      format.html { redirect_to products_url, notice: "Successfully destroyed product. #{undo_link}" }
      format.json { head :no_content }
    end
  end

  def import
    Product.import(params[:file])
    redirect_to root_url, notice: "Products imported."
  end

  private
    # Use callbacks to share common setup or constraints between actions.
    def set_product
      @product = Product.find(params[:id])
    end

    # Never trust parameters from the scary internet, only allow the white list through.
    def product_params
      params.require(:product).permit(:name, :price)
    end

    def undo_link
      view_context.link_to("undo", revert_version_path(@product.versions.scoped.last), :method => :post)
    end
end

和这里的布局文件:

<!DOCTYPE html>
<html>
<head>
  <title>Store</title>
  <%= stylesheet_link_tag    "application", media: "all", "data-turbolinks-track" => true %>
  <%= javascript_include_tag "application", "data-turbolinks-track" => true %>
  <%= csrf_meta_tags %>
</head>
<body>
    <div id="container">
        <% flash.each do |name, msg|%>
            <%= content_tag :div, raw(msg), :id => "flash_#{name}" %>
        <% end %>

        <%= yield %>
    </div>
</body>
</html>

我希望显示一次消息,但它显示两次,所以请告诉我我的错误在哪里?

4

1 回答 1

0

您在创建操作和视图中都调用了一条消息。

创建动作:

format.html { redirect_to @product, notice: "Product was successfully created. #{undo_link}" }

看法:

 <% flash.each do |name, msg|%>
   <%= content_tag :div, raw(msg), :id => "flash_#{name}" %>
  <% end %>

第一个给出:

 Product was successfully created. undo

后者显示所有原始输出,因为 raw(msg):

Product was successfully created. <a data-method="post" href="/versions/148/revert" rel="nofollow">undo</a>
于 2014-04-08T04:39:54.040 回答