0

キャンペーンとメッセージがあります。キャンペーンには多くのメッセージがあり、メッセージはキャンペーンに属します。モデルとビューでネストされた属性をセットアップし、モデルに関連付けられたメッセージを作成しようとしています。コードは次のとおりです。

class Campaign < ActiveRecord::Base
  attr_accessible :message_id, :name, :group_id, :user_id, :messages_attributes

  has_many :messages
  belongs_to :group
  belongs_to :user

  accepts_nested_attributes_for :messages
end

class Message < ActiveRecord::Base
  attr_accessible :body, :sent, :sent_at

  belongs_to :user
  belongs_to :campaign
  has_many :responses

end

そしてフォーム:

= form_for @campaign, :html => {:class => 'form-horizontal'} do |f|
...removed error output code...
  %legend
    Enter the campaign information
  .field
    .control-group
      %label.control-label
        Campaign Name
      .controls
        = f.text_field :name
        = f.collection_select(:group_id, current_user.groups.all, :id, :name, :include_blank => true)
        = f.fields_for :message do |m|
          = m.text_area :body, :rows => 3
      .form-actions= f.submit "#{params[:action] == 'new' ? 'Create New Campaign' : 'Save Campaign'}", :class => 'btn btn-success'

おそらく非常に単純なことだと思いますが、メッセージで大量割り当ての問題が発生し続けます。エラーは次のとおりです。

ActiveModel::MassAssignmentSecurity::Error (Can't mass-assign protected attributes: message):
  app/controllers/campaigns_controller.rb:18:in `update'

最後に、フォームから作成されるパラメーター:

  Parameters: {"utf8"=>"✓", "authenticity_token"=>"(removed)", "campaign"=>{"name"=>"Weekly Poker", "group_id"=>"1", "message"=>{"body"=>"s"}}, "commit"=>"Save Campaign", "id"=>"1"}
4

1 に答える 1

0

これはhas_many連想なので、複数形にする必要があります。

= f.fields_for :messages do |m|

また、コントローラーには次のものが必要です。

def new
  @campaign = Campaign.new
  @campaign.messages.build
end
于 2012-11-07T17:10:15.183 に答える