【发布时间】: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