嵌套表单质量分配错误与a有很多关系

时间:2012-07-17 16:36:23

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

我正在尝试使用嵌套表单在创建歌曲时为歌曲添加类别标签。目前,每当我提交表单时,它都会抛出一个质量分配错误,尽管事实上我已经放入了我认为正确的属性可访问特征。显然我在某个地方出了问题所以如果有人能为我指出这一点会很棒。 表格是:

<%= nested_form_for(@song) do |f| %>
         ...
      <%= f.fields_for(@song.categorizations.build) do |cat| %>
        <%= cat.label :category_id, "TAG" %>
        <%= cat.select :category_id, options_for_select(Category.all.collect {|c| [ c.tag, c.id ] }, 
        { :include_blank => true }), prompt: "" %>
      <%end%>
      <%= f.submit "Save" %>
    <% end %>

这里的相关模型是:

class Song < ActiveRecord::Base
  attr_accessible :artist, :title, :categorizations_attributes
  has_many :categorizations, dependent: :destroy
  has_many :categories, through: :categorizations

  accepts_nested_attributes_for :categorizations

歌曲控制器如下所示:

 def create
    @song = Song.new(params[:song])
    if @song.save
        flash[:success] = "Song successfully added to library"
        redirect_to @song
    else
        #FAIL!
        render 'new'
    end
  end

最后引发的错误是:

ActiveModel::MassAssignmentSecurity::Error in SongsController#create

Can't mass-assign protected attributes: categorization

提前感谢您的帮助!

0 个答案:

没有答案