【问题标题】:Rails - MassAssignmentSecurity:: Error on multiple photo uploadRails - MassAssignmentSecurity:: 多张照片上传错误
【发布时间】:2013-06-01 11:12:58
【问题描述】:

我想为一个小应用制作一个产品页面。此产品页面应允许用户添加多张照片。所以自然有三种模式。用户、产品和照片。用户有_many 产品和产品有_many 照片。

一切都很花哨,但每当我尝试添加许多照片时,我都会收到此错误。

ActiveModel::MassAssignmentSecurity::Error in ProductsController#create

Can't mass-assign protected attributes: photos_attributes

产品控制器

  def new 
    @product = Product.new
    @photo = Photo.new
    4.times{ @product.photos.build }
  end

  def create
  @product = current_user.products.new(params[:product])
  @photo = current_user.photos.new(params[:photo])

    if @product.valid? && @photo.valid?
      @product.save
      @photo.product_id = @product.id
      @photo.save
        render "show", :notice => "Sale created!"
    else
        render "new", :notice => "Somehting went wrong!"
    end
  end

新产品页面(HAML)

= form_for @product,:url => products_path, :html => { :multipart => true } do |f|
  - if @product.errors.any?
    .error_messages
      %h2 Form is invalid
      %ul
        - for message in @product.errors.full_messages
          %li
            = message
  - if @photo.errors.any?
    .error_messages
      %h2 Image is invalid
      %ul
        - for message in @photo.errors.full_messages
          %li
            = message
  %p
    = f.label :name
    = f.text_field :name
  %p
    = f.label :description
    = f.text_field :description
  %p
    = f.fields_for :photos do |fp|
      =fp.file_field :image
      %br

  %p.button
    = f.submit

产品型号

class Product < ActiveRecord::Base
  attr_accessible :description, :name, :price, :condition, :ship_method, :ship_price, :quantity, :photo
  has_many :photos, dependent: :destroy
  accepts_nested_attributes_for :photos
  belongs_to :user
end

照片模型

class Photo < ActiveRecord::Base
  attr_accessible :image
  belongs_to :product
    has_attached_file :image, styles: { medium: "320x240>", :thumb => "100x100>"}
end

schema.rb

  create_table "photos", :force => true do |t|
    t.integer  "product_id"
    t.datetime "created_at",         :null => false
    t.datetime "updated_at",         :null => false
    t.string   "image_file_name"
    t.string   "image_content_type"
    t.integer  "image_file_size"
  end

  create_table "products", :force => true do |t|
    t.string   "name"
    t.text     "description"
    t.datetime "created_at",  :null => false
    t.datetime "updated_at",  :null => false
    t.integer  "user_id"
  end

编辑:当我将 :photos_attributes 添加到产品模型 attr_accessible 时,我收到一条错误消息,提示图像不能为空白!!

【问题讨论】:

    标签: ruby-on-rails ruby ruby-on-rails-3


    【解决方案1】:

    只需将photos_attributes 添加到产品模型中的attr_accessible 列表即可。或者更好的是,尝试迁移到strong_parameters gem,这会在您迁移到 Rails 4 时为您节省一些时间。

    更新

    create 操作中,您从params[:photo] 创建了一个@photo 变量,它知道返回nil,从而始终产生无效的对象。也就是说,您不需要创建该变量,因为 Photo 对象已经为您创建,因为您已经使用 accepts_nested_attributes_for。我知道您需要知道Photos 是否创建成功,但您不必担心,因为产品新照片的错误会传播到产品,从而停止保存过程。

    【讨论】:

    • 当我将 :photos_attributes 添加到产品模型 attr_accessible 时,我收到一条错误消息,提示图像不能为空白!!
    • 在我拥有的表格的开头 - 如果@photo.errors.any? .error_messages %h2 图片无效
    • 能否请您发布此行的输出@photos.errors.full_messages?或者“图像不能为空白”是它的输出?
    • 出于某种原因,这是输出
    • 查看编辑。我用错误消息填写了我的新产品页面的外观
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-05-06
    • 2012-08-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-05-21
    相关资源
    最近更新 更多