간단한 예제를 스캐 폴딩하여 문제를 설명합니다. 이 예에서는 우주선과 파일럿이 있습니다. 나는 창조 할 때 우주선에 기존의 조종사를 배치 할 수 있기를 원한다.선택 태그를 사용할 때 모델 (# ...)이 예상 됨 (# ...) 오류가 발생했습니다.
starship.rb
class Starship < ApplicationRecord
has_one :pilot
validates :name, presence: true
end
pilot.rb
class Pilot < ApplicationRecord
belongs_to :starship, optional: true
validates :name, presence: true
end
우주선/_form.html.erb
<div class="field">
<%= f.label :pilot %>
<%= f.select :pilot, Pilot.all %>
</div>
starships_controller.rb
def starship_params
params.require(:starship).permit(:name, :pilot)
end
PARAMS 해시
{"name"=>"Nostromo", "pilot"=>"#<Pilot:0x007f85ff547f90>"}
그리고이 오류를 얻을
Pilot(#70106745549840) expected, got String(#70106709663840)
내가, 내 파일럿이 해시의 문자열로 전송되는 것을 볼 수 있지만 나는 그것이 어떻게해야 하는지를 찾지 못하는 것 같습니다.
그런 매개 변수 해시에 객체를 전달할 수 없습니다. 당신은 그 객체의'id'를 보내고 나중에 목적지의 객체를 찾을 수 있습니다. –