【问题标题】:I can't upload multiple image using CarrierWave and Ruby on Rails我无法使用 CarrierWave 和 Ruby on Rails 上传多个图像
【发布时间】:2014-06-04 04:38:39
【问题描述】:

我在尝试使用多重上传文件时遇到了问题。

这是我的表单部分视图:

<%= 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?
                        }

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

【问题讨论】:

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


    【解决方案1】:

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

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

    其中 attachment_id 是产品附带的附件的 ID。

    【讨论】:

    • 抱歉,但我无法理解这如何解决这样一个事实:每次编辑我的产品时,附件集合都具有完全相同的对象(带有图像的原始对象和相同数量的空对象) ,我想我必须防止在 image 属性中保存带有 null 的附件,但我无法弄清楚如何。无论如何,我会尝试你所说的并让你知道。
    【解决方案2】:

    好的,查看 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>
    

    这样做我终于解决了这两个问题,无论如何感谢您的帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-05-09
      • 2014-07-17
      • 2018-04-18
      • 2019-08-22
      • 1970-01-01
      相关资源
      最近更新 更多