我无法使用CarrierWave和Ruby on Rails上传多个图像

时间:2014-04-19 20:12:59

标签: ruby-on-rails image file-upload carrierwave

我尝试使用多重上传文件时遇到了麻烦。

这是我的表格局部视图:

<%= simple_form_for @product, :html => { :multipart => true, :class => 'form-horizontal'} do |f| %>
  <%= f.input :name %>
  <%= f.input :description %>
  <%= f.input :price %>
  <%= f.input :isenable, check_box_html: { class: 'check_box' }  %>
  <div class="row-fluid">
    <ul class="thumbnails">
      <%= f.simple_fields_for :attachments do |attachments_form| %>
        <li class="span4">
          <div class="thumbnail">
              <%= image_tag attachments_form.object.image_url(:thumb).to_s if attachments_form.object %>
              <%= attachments_form.label :_destroy, "Borrar Imagen" %>
              <%= attachments_form.file_field :image %>
          </div>
        </li>
      <% end %>
    </ul>
  </div>
  <%= f.button :submit %>
  <%= link_to t('.cancel', :default => t("helpers.links.cancel")),
                products_path, :class => 'btn' %>

<% end %>

这是我的控制者:

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


  # GET /products
  # GET /products.json
  def index
    @products = Product.all
  end

  # GET /products/list
  # GET /products/list.json
  def list
    @products = Product.all
  end

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

  # GET /products/new
  def new
    @product = Product.new
    3.times do
        attachments = @product.attachments.build
    end
  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.' }
        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, notice: 'Product was successfully updated.' }
        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 }
      format.json { head :no_content }
    end
  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, :description, :price, :isenable, attachments_attributes: [:image, :remove_image])
    end
end

这是节目文件:

<%- model_class = Product -%>
<div class="page-header">
  <h1><%=t '.title', :default => model_class.model_name.human.titleize %></h1>
</div>

<dl class="dl-horizontal">
  <dt><strong><%= model_class.human_attribute_name(:name) %>:</strong></dt>
  <dd class="lead"><%= @product.name %></dd>
  <dt><strong><%= model_class.human_attribute_name(:description) %>:</strong></dt>
  <dd class="lead"><%= @product.description %></dd>
  <dt><strong><%= model_class.human_attribute_name(:price) %>:</strong></dt>
  <dd class="lead"><%= @product.price %></dd>
  <dt><dt>
</dl>
<div class="row-fluid">
  <ul class="thumbnails">
    <% @product.attachments.each do |picture| %>
      <li class="span4">
        <div class="thumbnail">
          <%= image_tag picture.image_url(:full).to_s %>
        </div>
      </li>
    <% end %>
  </ul>
</div>
<div class="form-actions">
  <%= link_to t('.back', :default => t("helpers.links.back")),
              products_path, :class => 'btn'  %>
  <%= link_to t('.edit', :default => t("helpers.links.edit")),
              edit_product_path(@product), :class => 'btn' %>
  <%= link_to t('.destroy', :default => t("helpers.links.destroy")),
              product_path(@product),
              :method => 'delete',
              :data => { :confirm => t('.confirm', :default => t("helpers.links.confirm", :default => 'Are you sure?')) },
              :class => 'btn btn-danger' %>
</div>

我正在使用Rails 4.0.0 ruby​​ 2.0.0p247,正如我所说我使用CarrierWave上传文件,如果我将图像留空,它会在创建屏幕中创建一个空的图像对象。如果用户不添加文件,我想要的是不创建图像。 我的另一个问题是与删除链接有关。它很简单并不起作用。有什么建议吗?


我已经部分解决了将以下行添加到我的产品型号中的问题:

accepts_nested_attributes_for :attachments, :allow_destroy => true,
                        :reject_if => lambda {
                          |a| a['image'].blank?
                        }

它允许我在没有文件的情况下插入任何图片。所以我的问题的一部分已经解决了我需要解决的问题是删除链接不起作用。

2 个答案:

答案 0 :(得分:1)

好的回顾CarrierWave的文档,主要是Rails文档,我发现了问题的解决方法。正如我之前所说的防止插入文件为空的解决方案所述的第一个是在我的产品模型中包含以下行:

accepts_nested_attributes_for :attachments, :allow_destroy => true,
                        :reject_if => lambda {
                          |a| a['image'].blank?
                        }

为了解决CarrierWave没有删除图像的问题,我发现当你使用嵌套属性时,你应该在你的控制器中包含:id和:_destroy作为允许,如下所示:

# permit :id and :_destroy

    params.require(:author).permit(:name, books_attributes: [:title, :id, :_destroy])

这正是我在控制器中所做的,也是我在视图中更改了复选框的名称。所以简言之,控制器是这样的:

def product_params
  params.require(:product).permit(:name, :description, :price, :isenable, attachments_attributes: [:image, :_destroy, :id])
end

并且视图就是这样:

<label><%= attachments_form.check_box :_destroy %>Borrar Imagen</lable>

这样做我最终解决了这两个问题,无论如何都要感谢你的帮助。

答案 1 :(得分:0)

在您的创建操作中,您必须创建附件,您必须执行以下操作..

  @product.attachments.create(:attachment_id).save

其中attachment_id是随产品附加的附件的ID。