0

私のサーバーには、次の 2 つのモデルがあります。

ブロードキャスト

class Broadcast < ActiveRecord::Base

  validates_presence_of :content

  belongs_to :user

  has_and_belongs_to_many :feeds

  attr_accessible :content, :feeds, :feeds_attributes

end

class Feed < ActiveRecord::Base
  has_and_belongs_to_many :broadcasts
  attr_accessible :name
end

私のクライアントには、これらのモデル用の基本的な ActiveResource クラスがあります。

指定されたフィード (クライアントから) で新しいブロードキャストを作成しようとすると:

feed = Feed.find(3) <-succesful

broadcast = Broadcast.new
broadcast.attributes['feed'] ||= [] 
broadcast.feed << feed
broadcast.save

サーバー上の BroadcastController では、単純に

@broadcast = Broadcast.new(params[:broadcast])

次のエラーが発生します。

ActiveRecord::AssociationTypeMismatch (Feed(#45931224) が予想され、ActiveSupport::HashWithIndifferentAccess(#25685616) を取得):

変えてみた

broadcast.attributes['feed'] ||= [] 

broadcast.attributes['feed_attributes'] ||= [] 

しかし、「不明な属性エラー」が表示されました</p>

4

1 に答える 1

0

気にしないでください、私は行方不明でした:

accepts_nested_attributes_for :feeds

私の Broadcast メソッドで。

とにかく、誰かがこの質問に遭遇した場合は、次のことに注意してください。

broadcast.attributes['feed_attributes'] ||= [] 

「_attributes」サフィックスがなくても、HashWithIndifferentAccess エラーは引き続き発生します。

于 2012-11-27T18:44:58.647 に答える