ruby-on-rails-4 – 无法使用Paperclip在Rails 4中保存图像属性
我的Rails 4应用程序中有两个相关的模型:product.rb和image.rb.
Image模型允许使用Paperclip gem附加文件.
图像属于产品,产品具有多个图像. 我想在创建产品时使用产品的新视图来创建和附加图像.每次我尝试时,与Paperclip图像相关的参数都不会被保存,最终结果为零. 以下是模型: Product.rb class Product < ActiveRecord::Base validates :name,:part_number,presence: true has_many :images,dependent: :destroy belongs_to :category accepts_nested_attributes_for :images,allow_destroy: true end Image.rb class Image < ActiveRecord::Base belongs_to :product has_attached_file :image end 回顾过去的Stack Overflow问题,我发现了一些相关的问题和答案,但没有一个似乎对我的特殊情况有所帮助.到目前为止,我能够使用正确的属性提交文件,但它不会将它们保存到数据库中. ProductsController的#创建 def create @product = Product.new(product_params) end def product_params params.require(:product).permit(:name,:category_id,images_attributes: [:image]) end ProductsController的#新 def new @product = Product.new @categories = # irrelevant to question end 产品/ new.html.haml = form_for @product,:html => = f.label :name = f.text_field :name = f.label :part_number = f.text_field :part_number = f.label :category_id = f.select :category_id,@ca = f.fields_for :image do |ff| = ff.label :image = ff.file_field :image = f.submit('Create Product') 查看参数我可以告诉正确的属性被传递: 示例参数: {"utf8"=>"?","authenticity_token"=>"jGNy/TasHzP0EcWDFzZ3XH5/fpxb6vD+ncQ2PZSQ3/I=","product"=>{"name"=>"bar","part_number"=>"foo","category_id"=>"30","image"=>{ "image"=>#<ActionDispatch::Http::UploadedFile:0x007fc82f58e0e0 @tempfile=#<Tempfile:/var/folders/04/23d9grkj5fv3wkj2t8858kx40000gn/T/RackMultipart20131029-64949-1ldkz4g>,@original_filename="FPE230_b.jpg",@content_type="image/jpeg",@headers="Content-Disposition: form-data; name="product[image][image]"; filename="FPE230_b.jpg"rnContent-Type: image/jpegrn">}},"commit"=>"Create Product"} 但是,图像实际上并未保存到数据库中.我怎么能纠正这个? 编辑 仔细查看日志,我发现我收到了“未经许可的参数:图像”错误.即使将images_attributes:[:image]添加到我的product_params方法后也是如此. 解决方法
我能够得到类似于你的设置,使用以下更改:
在ProductsController #create中,不是单独构建图像,而是让嵌套属性处理它,然后执行: @product = Product.new(product_params) 并允许嵌套参数(我从this question中找出了image_attributes:[:image]): def product_params params.require(:product).permit(:name,images_attributes: [:image]) end 这是我的参数在创建请求的日志中的样子: { "utf8"=>"?","authenticity_token"=>"7EsPTNXu127itgp4fbohu672/L4XnSwLEkrqUGec3pY=","product"=>{ "name"=>"whatever","part_number"=>"12345","images_attributes"=>{ "0"=>{ "image"=>#<ActionDispatch::Http::UploadedFile:0x007feb0c183b08 @tempfile=#<Tempfile:/var/folders/6k/16k80dds0ddd6mhvs5zryh3r0000gn/T/RackMultipart20131031-51205-emaijs>,@original_filename="some-image.png",@content_type="image/png",@headers="Content-Disposition: form-data; name="product[images_attributes][0][image]"; filename="ponysay.png"rnContent-Type: image/pngrn"> } } },"commit"=>"Create"} 我看到插入产品和插入图像. 如果这不起作用,你可以发布你的ProductsController#new和你的新产品表格吗? 编辑后编辑: 我的app / views / products / new.html.erb和ProductsController #new中有3个不同的东西可能会影响您的结果: >在form_for中,我有多部分:真如paperclip documentation: <%= form_for @product,:url => products_path,:html => { :multipart => true } do |f| %> >因为产品has_many:图像,在我的表单中我有fields_for:images而不是fields_for:image: <%= f.fields_for :images do |i| %> <%= i.file_field :image %> <% end %> >这个fields_for在页面上没有显示任何内容,除非在ProductsController#new中我构建了一个图像;你的Product.new有没有做到这一点? def new @product = Product.new @product.images.build end 你有这些差异有什么特别的原因吗?如果你改变它以匹配我的代码,你的代码是否有效? (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |