将许多照片添加到记录中。无法批量分配受保护的属性:

时间:2012-07-01 12:30:54

标签: ruby-on-rails activerecord paperclip mass-assignment

我正在尝试使用回形针将照片添加到我的应用中。一个杯子可以有很多照片。

当我尝试保存新杯子时,我收到了这个错误:

  

ActiveModel :: MassAssignmentSecurity :: MugsController中的错误#create

     

无法大量分配受保护的属性:mugphoto

Mug.rb

class Mug < ActiveRecord::Base
  attr_accessible :name, :mugphotos_attributes
  has_many :mugphotos
  accepts_nested_attributes_for :mugphotos, :allow_destroy => true
end

Mugphoto.rb

class Mugphoto < ActiveRecord::Base
  belongs_to :mug

  has_attached_file :mugphoto, 
    :styles => {  
      :thumb => "100x100#" }
end

马克杯new.html.erb

<%= form_for @mug, :html => { :multipart => true } do |f| %>

  <p>name: <br>
  <%= f.text_field :name %></p>

  <%= f.fields_for :mugphoto do |photo| %>
    <p>photo: <br>
    <%= photo.file_field :mugphoto %></p>
  <% end %>     

  <div class="button"><%= submit_tag %></div>
<% end %>

mugs_controller

class MugsController < ApplicationController

  def new
    @mug = Mug.new
  end

  def create
    @mug = Mug.create(params[:mug])
    if @mug.save
      flash[:notice] = 'Mug added'
      redirect_to mugs_path
    else
      render :action => :new
    end
  end
end

1 个答案:

答案 0 :(得分:1)

你需要在Mugphoto.rb中添加attr_accessible:mugphoto