Rails:尝试将两个新模型从一种形式保存为另一种形式的 FK
Rails: Trying to save two new models with one as a FK to the other from one form
好吧,有可能我只需要睡觉,但我已经睡了一段时间了。这是我的。
我有 create/manage 个团队的申请。这些球队有一个可以管理球队的队长。当用户创建新团队时,他们应该自动创建为团队队长。
我认为应该比我自己制作的链接更有用的参考链接:
- Merge two forms(两个模型合二为一)
- Rails nested form with has_many :through, how to edit attributes of join model?
- Building complex forms
我觉得我 运行 遇到的问题是,在我的情况下,我需要创建团队和队长并将它们联系在一起。在这些链接中,团队似乎已经创建并且队长将以单独的形式绑定到它。
相关代码
型号:
team.rb
has_many :captain
accepts_nested_attributes_for :captain
captain.rb
belongs_to :team
团队形式:
<%= form_for(@team) do |f| %>
<%= f.fields_for @team.captain do |tf| %>
<%= render 'captain/form', locals: { form: tf } %>
<% end %>
<%= f.label :name %>
<%= f.text_field :name, :required => true %>
<%= f.label :team_color%>
<%= f.text_field :team_color, :required => true %>
<%= f.submit %>
<% end %>
队长形式:
<%= f.hidden_field :user_id, value: current_user.id %>
<p>
<%= f.label :auto_search %>
<%= f.check_box :auto_search %>
</p>
<p>
<%= f.label :sub_list_first %>
<%= f.check_box :sub_list_first %>
</p>
<p>
<%= f.label :degrees_of_separation %>
<%= f.number_field :degrees_of_separation %>
</p>
<p>
<%= f.label :search_timer %>
<%= f.number_field :search_timer, value: 0 %>
</p>
teams_controller.rb
def create
@team = Team.new(team_params)
respond_to do |format|
if @team.save
# These lines added during edit
@team.captain_users.build
@team.captain_users.first.save
format.html { redirect_to @team, notice: 'Team was successfully created.' }
format.json { render json: @team.id }
else
format.html { render :new }
format.json { render @team.errors, status: :unprocessable_entity }
end
end
end
def team_params
# I think I need captain_attributes here also?
params.require(:team).permit(:name, :team_color)
end
这会以与团队字段相同的形式显示队长字段。但是从这里点击提交什么都不做。如果我问的是可能的,我什至不肯定。现在(忽略一些 ajax/remote 电话),我正在考虑创建一个团队,然后创建一个队长并添加一些通知或重定向到队长设置。
编辑:我现在似乎可以使用它了。我遵循了 this 指南。我仍然不确定发生了什么,但我很高兴它现在已经清理干净了。
将team
和captain
之间的关系称为[=22是很合理的=]
team ---> has_one :captain
而不是 has_many
#team.rb
has_one :captain
accepts_nested_attributes_for :captain
然后在您的 new
方法中添加此行 @team.build_captain
并在您的表单中,将此行 <%= f.fields_for @team.captain_user do |tf| %>
更改为 <%= f.fields_for :captain do |tf| %>
更新:
在 team_params
中包含 captain_attributes
def team_params
params.require(:team).permit(:name, :team_color, captain_attributes: [:id, :auto_search, :sub_list_first, degrees_of_separation, :search_timer])
end
好吧,有可能我只需要睡觉,但我已经睡了一段时间了。这是我的。
我有 create/manage 个团队的申请。这些球队有一个可以管理球队的队长。当用户创建新团队时,他们应该自动创建为团队队长。
我认为应该比我自己制作的链接更有用的参考链接:
- Merge two forms(两个模型合二为一)
- Rails nested form with has_many :through, how to edit attributes of join model?
- Building complex forms
我觉得我 运行 遇到的问题是,在我的情况下,我需要创建团队和队长并将它们联系在一起。在这些链接中,团队似乎已经创建并且队长将以单独的形式绑定到它。
相关代码
型号:
team.rb
has_many :captain
accepts_nested_attributes_for :captain
captain.rb
belongs_to :team
团队形式:
<%= form_for(@team) do |f| %>
<%= f.fields_for @team.captain do |tf| %>
<%= render 'captain/form', locals: { form: tf } %>
<% end %>
<%= f.label :name %>
<%= f.text_field :name, :required => true %>
<%= f.label :team_color%>
<%= f.text_field :team_color, :required => true %>
<%= f.submit %>
<% end %>
队长形式:
<%= f.hidden_field :user_id, value: current_user.id %>
<p>
<%= f.label :auto_search %>
<%= f.check_box :auto_search %>
</p>
<p>
<%= f.label :sub_list_first %>
<%= f.check_box :sub_list_first %>
</p>
<p>
<%= f.label :degrees_of_separation %>
<%= f.number_field :degrees_of_separation %>
</p>
<p>
<%= f.label :search_timer %>
<%= f.number_field :search_timer, value: 0 %>
</p>
teams_controller.rb
def create
@team = Team.new(team_params)
respond_to do |format|
if @team.save
# These lines added during edit
@team.captain_users.build
@team.captain_users.first.save
format.html { redirect_to @team, notice: 'Team was successfully created.' }
format.json { render json: @team.id }
else
format.html { render :new }
format.json { render @team.errors, status: :unprocessable_entity }
end
end
end
def team_params
# I think I need captain_attributes here also?
params.require(:team).permit(:name, :team_color)
end
这会以与团队字段相同的形式显示队长字段。但是从这里点击提交什么都不做。如果我问的是可能的,我什至不肯定。现在(忽略一些 ajax/remote 电话),我正在考虑创建一个团队,然后创建一个队长并添加一些通知或重定向到队长设置。
编辑:我现在似乎可以使用它了。我遵循了 this 指南。我仍然不确定发生了什么,但我很高兴它现在已经清理干净了。
将team
和captain
之间的关系称为[=22是很合理的=]
team ---> has_one :captain
而不是 has_many
#team.rb
has_one :captain
accepts_nested_attributes_for :captain
然后在您的 new
方法中添加此行 @team.build_captain
并在您的表单中,将此行 <%= f.fields_for @team.captain_user do |tf| %>
更改为 <%= f.fields_for :captain do |tf| %>
更新:
在 team_params
captain_attributes
def team_params
params.require(:team).permit(:name, :team_color, captain_attributes: [:id, :auto_search, :sub_list_first, degrees_of_separation, :search_timer])
end