0

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

当我尝试保存新杯子时,出现此错误:

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

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

杯子.rb

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

大头照.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
4

1 回答 1

1

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

于 2012-07-01T13:33:52.137 回答