我使用paperclip gem在我的应用程序中实现多个图像上传。 我的模型如下
class ProductImage < ActiveRecord::Base
belongs_to :product
has_attached_file :image ,:styles => {
:thumb => ['100x100#', :jpg, :quality => 70],
:preview => ['480x480#', :jpg, :quality => 70],
:large => ['600>', :jpg, :quality => 70],
:retina => ['1200>', :jpg, :quality => 30]
},:path => ':rails_root/public/system/:id.:extension',
:convert_options => {
:thumb => '-set colorspace sRGB -strip',
:preview => '-set colorspace sRGB -strip',
:large => '-set colorspace sRGB -strip',
:retina => '-set colorspace sRGB -strip -sharpen 0x0.5'
},
:url => "/assets/books/:id/:style/:basename.:extension",
:path => ":rails_root/public/assets/books/:id/:style/:basename.:extension"
validates_attachment_presence :image
validates_attachment_size :image , :less_than => 10.megabytes
validates_attachment_content_type :image , :content_type => ['image/jpeg','image/jpg','image/png']
do_not_validate_attachment_file_type :image
end
class Product < ActiveRecord::Base
belongs_to :user
belongs_to :category
has_many :comments , dependent: :destroy
has_many :product_images, :dependent => :destroy
accepts_nested_attributes_for :product_images, :reject_if => lambda { |t| t['product_image'].nil? }
end
product_controller.rb
class ProductsController < ApplicationController
def new
@product = current_user.products.new
3.times {@product.product_images.build}
end
def create
@product = current_user.products.new
3.times { @product.product_images.build }
respond_to do |format|
if @product.save
format.html { redirect_to @product, notice: 'Product was successfully created.' }
format.json { render :show, status: :created, location: @product }
else
format.html { render :new }
format.json { render json: @product.errors, status: :unprocessable_entity }
end
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, :price, :description, :reason, :user_id,:status,:category_id,product_images_attributes: [:image])
end
def correct_user
@product = current_user.products.find_by(id: params[:id])
redirect_to root_url if @product.nil?
end
end
_form.html.erb
<%= form_for @product, url: products_path, :html => {:multipart => true,:class => " form-horizontal center"} do |f| %>
<%= f.fields_for :product_images do |builder| %>
<p>
<%= builder.label :image, "Image File" %>
<%= builder.file_field :image %>
</p>
<% end %>
<div class="actions">
<%= f.submit "Submit", class: "btn btn-default btn-primary" %>
</div><br/>
<% end %>
show.html.erb
<% @products.each do |product| %>
<% product.product_images.each do |picture| %>
<%= picture.image.url %>
<% end %>
当我提交文件页面没有被重定向到主页。我甚至看不到错误。可以有人解释我在代码中的问题
答案 0 :(得分:0)
我注意到的一件事是产品控制器创建动作:
@product = current_user.products.new
应该是:
@product = current_user.products.build(product_params)
在创建操作中创建新对象时,应始终传入product_params。