【问题标题】:Ruby on Rails 4 and PaperclipRuby on Rails 4 和回形针
【发布时间】:2015-11-14 10:53:50
【问题描述】:

我正在 Rails 中构建一个项目,它是一家公司的虚拟商店。用户应该可以注册新产品并上传图片,所以我生成了 2 个模型:“Product”和“Image”,Product has_many Images 和一个 Image belongs_to product,我已经运行了回形针来更新 Image。 “accepts_nested_attributes_for :images”在产品模型中,所以我可以保存同一产品表单中的图像。问题是图像没有被保存。用户应该能够上传 3 张图片(这是我允许它“3.times {@product.images.build}”的方式)。这是我的产品控制器:

def new
    @product = Product.new
    3.times {@product.images.build}
 end

 def create
     @product = Product.new(params[:product])
     if @product.save
        redirect_to :root
     else
        render :action => 'new'
     end
 end

我也尝试像这样定义 product_params 方法:

private
    def product_params
        params.require(:product).permit(:title, :info, :description, :price, :images_attributes => [])
    end

这是控制台抛出的Request信息:

Request

Parameters:

{"utf8"=>"✓",
 "authenticity_token"=>"qRyYjG9pSaFxgCtMddDN3fpbsTeIAagLEz+psd+Z+oHa2AVjXpXYcbxta/Egj2TGrmF3FFNCllkY54dig3aN8g==",
 "product"=>{"title"=>"Title",
 "info"=>"Infor",
 "description"=>"Description",
 "price"=>"9",
 "images_attributes"=>{"0"=>{"photo"=>#<ActionDispatch::Http::UploadedFile:0xad3d830 @tempfile=#<Tempfile:/tmp/RackMultipart20150820-9643-h6wdta.png>,
 @original_filename="sticker,
375x360.u1.png",
 @content_type="image/png",
 @headers="Content-Disposition: form-data; name=\"product[images_attributes][0][photo]\"; filename=\"sticker,
375x360.u1.png\"\r\nContent-Type: image/png\r\n">},
 "1"=>{"photo"=>#<ActionDispatch::Http::UploadedFile:0xad3d704 @tempfile=#<Tempfile:/tmp/RackMultipart20150820-9643-19t1r3v.png>,
 @original_filename="sticker,
375x360.u1.png",
 @content_type="image/png",
 @headers="Content-Disposition: form-data; name=\"product[images_attributes][1][photo]\"; filename=\"sticker,
375x360.u1.png\"\r\nContent-Type: image/png\r\n">},
 "2"=>{"photo"=>#<ActionDispatch::Http::UploadedFile:0xad3d650 @tempfile=#<Tempfile:/tmp/RackMultipart20150820-9643-fj3w5g.png>,
 @original_filename="sticker,
375x360.u1.png",
 @content_type="image/png",
 @headers="Content-Disposition: form-data; name=\"product[images_attributes][2][photo]\"; filename=\"sticker,
375x360.u1.png\"\r\nContent-Type: image/png\r\n">}}},
 "commit"=>"Submit"}

如果我将方法product_params 作为参数传递给@product = Product.new(product_params),它会保存标题、描述和价格,但不会保存图像。

【问题讨论】:

    标签: ruby-on-rails ruby image ruby-on-rails-4 paperclip


    【解决方案1】:

    你保留了:images_attributes =&gt; [],但它应该是:images_attributes =&gt; [:image]

    所以,改变这个:

    params.require(:product).permit(:title, :info, :description, :price, :images_attributes => [])
    

    到:

    params.require(:product).permit(:title, :info, :description, :price, :images_attributes => [:image])
    

    在您的 product_params 方法中。

    之后应该可以工作了!

    对于图像验证,请使用:

    validates_attachment_presence :image
    

    如果此验证不起作用,请尝试使用这样的自定义验证:

      validate :image_present
    
      def image_present
        if image_file_name.blank?
          errors.add(:image, :image not present")
        end
      end
    

    【讨论】:

    • 哦,谢谢!现在它正在保存,但不知何故它在图像表中保存了 nil 值
    • 认为您需要为此添加验证。
    • 我有:has_attached_file :photo 和 validates_attachment_presence :photo 但它说 Unpermitted parameter: photo in the console。
    • 看看我更新的答案。添加该自定义验证而不是您的验证,看看是否有效。让我知道。
    • 等等,。你有? photo?将其更改为image?因为你添加了:accepts_nested_attributes_for :images
    猜你喜欢
    • 1970-01-01
    • 2017-01-14
    • 1970-01-01
    • 2015-06-26
    • 1970-01-01
    • 2013-04-15
    • 1970-01-01
    • 1970-01-01
    • 2016-12-19
    相关资源
    最近更新 更多