8
내 rest API로 모델을 저장하는 데 문제가 있습니다. 내가 관련된 카드 많은 작업과 모델 한 고객이 있습니다ActiveRecord :: AssociationTypeMismatch on model 저장
class Card < ActiveRecord::Base
belongs_to :customer
has_many :card_tasks
has_many :tasks, :through => :card_tasks
accepts_nested_attributes_for :tasks
accepts_nested_attributes_for :card_tasks
accepts_nested_attributes_for :customer
end
class CardTask < ActiveRecord::Base
belongs_to :task
belongs_to :card
accepts_nested_attributes_for :task
accepts_nested_attributes_for :card
end
class Task < ActiveRecord::Base
has_many :cards, :through => :card_tasks
has_many :card_tasks
end
나는이 같은 JSON 보낼 때 :
{
"card" = > {
"miscellaneous" = > "Obervations diverses",
"heater" = > "0",
"water_quality" = > "",
"customer" = > {
"id" = > "2", "house_name" = > "house_name2", "city" = > "city_2", "lastname" = > "lastname2", "sci" = > "sci2", "postal_code" = > "potal_code_2", "address_line_1" = > "address_line_2", "updated_at" = > "2012-03-05 18:20:57 +0000", "created_at" = > "2012-03-05 18:20:54 +0000", "firstname" = > "firstname2", "address_line_2" = > "address_line_3", "water_used" = > "0"
},
"tasks" = > [
{
"title" = > "Nettoyage ligne eau", "id" = > "6", "updated_at" = > "2012-02-17 08:40:47 +0000", "created_at" = > "2012-02-17 08:40:47 +0000"
},
{
"title" = > "Surveillance", "id" = > "4", "updated_at" = > "2012-02-17 08:40:47 +0000", "created_at" = > "2012-02-17 08:40:47 +0000"
}
]
}
}
행동 만들어 내 :
def create
card = Card.new(params[:card])
if (card.save)
respond_with({ :card => card} , :location => nil, status: :created) and return
end
respond_with({ :errors => card.errors }, :location => nil, status: :unprocessable_entity) and return
end
이 일을, 내가 가지고있어 :
ActiveRecord::AssociationTypeMismatch (Task(#70249431354580) expected, got ActiveSupport::HashWithIndifferentAccess(#70249421573300)):
app/controllers/cards_controller.rb:14:in `new'
app/controllers/cards_controller.rb:14:in `create'
W 모자 내가 잘못 했니?
감사합니다.하지만 지금은 ActiveRecord :: RecordNotFound (ID가있는 작업 = ID = 6)을 model.save()에 가지고 있습니다. – Sebastien
죄송합니다. 새 작업이 아니라 기존 작업을 볼 수 없습니다. . accepts_nested_attributes_for는 그 경우 쓸모가 없다고 추측합니다. 연결 변경을 허용하지 않습니다. nested_attributes.rb를 패치하거나 원숭이 패치를 만들거나 사용자 정의 처리기를 만들 수 있습니다. 다음은 구현되지 않은 이유에 대한 자세한 내용입니다. https://github.com/rails/rails/issues/2925 – dimuch
Card.new (params [: card])와 관련된 작업으로 새 카드를 만들 수 없습니다. 내 아들과? – Sebastien