0
키중첩 된 양식 : 업데이트 자녀 외국 나는 중첩 협회가
class User < ActiveRecord::Base
has_many :hostels
accepts_nested_attributes_for :hostels
end
class Hostel < ActiveRecord::Base
belongs_to :user
end
형태 :
<%= form_for @user do |f| %>
<%= f.label :email %><br>
<%= f.text_field :email %>
<% f.object.hostels << @hostel -%>
<%= f.fields_for :hostels do |ff| %>
<%= ff.hidden_field :id %>
<% end -%>
<%= f.submit %>
<% end -%>
컨트롤러를
def create
@user = User.new(user_params)
raise @user.hostels.inspect
end
private
def user_params
params.require(:user).permit(:email, hostels_attributes: [:id])
end
나는 기존의 기록을 다시 연결하고 싶은
호스텔 외래 키를 업데이트하여 새 사용자에게 호스텔을 제공합니다. 이 방법은 확실히 작동하지 않습니다.
시도한 update_only: true
매개 변수도 중첩됩니다.
제목에 대한 아이디어가 있습니까? 아니면 내가 그런 수술을 시도하는 데 완전히 틀렸습니까?