如何以相同的形式更新 table 及其关联的连接 table?
How do update a table and it's associated join table in the same form?
我正在为播客建立一个网站。我的 table 是 "episodes" 和 "guests" 以及一个名为 "episodes_guests" 的连接 table。 "episodes" 拥有并属于许多 "guests"。我有一个表格可以更新每集的信息,并且我试图让它也更新与该集相关的客人(数据存储在 join table 中),但无法弄清楚。
我已经能够添加一个 (form_for) 表单字段来填充相关的客人信息,但是当我点击更新时,这些字段不会更新连接 table(我什至没有确定我是否正在输出信息)。
这是来自剧集控制器 'edit' 视图的 table
<%= form_for(@episode) do |f| %>
<table summary="Episode form fields">
<tr>
<th> Episode ID </th>
<td><%= f.text_field(:id) %></td>
</tr>
<%= f.fields_for :guests, @episode.guests.each do |g| %>
<tr>
<!-- I am trying to add a selector for guests -->
<th>Guest: </th>
<td>
<%= g.text_area :last_name %>
</td>
</tr>
<% end %>
</table>
<div class="form-buttons">
<%= f.submit("Update Episode") %>
<% end %>
</div>
剧集模型
class Episode < ApplicationRecord
has_and_belongs_to_many :guests
accepts_nested_attributes_for :guests
scope:sorted, lambda {order("episode_number ASC")}
end
客座模特
class Guest < ApplicationRecord
has_and_belongs_to_many :episodes
scope:sorted, lambda {order("id ASC")}
end
剧集控制
class EpisodesController < ApplicationController
def edit
@episode = Episode.find(params[:id])
@guests = @episode.guests.all
end
def update
#Find a object using form parameters
@episode = Episode.find(params[:id])
@episode_guests = @episode.guests
#Update the object
if @episode.update_attributes(episode_params)
#If the save succeeds, redirect to the show actions
redirect_to(episode_path(@episode))
else
#If the save fails, redisplay the form so user can fix problems
render('edit')
end
end
private
def episode_params
params.require(:episode).permit(:id, :episode_number, :title, :guests, :file_name, :description, :date_released)
end
def guest_params
params.require(:guest).permit(:id, :guest_id, :first_name, :last_name)
end
end
来宾控制器
class GuestsController < ApplicationController
def edit
@guest = Guest.find(params[:id])
end
def update
#Find a object using form parameters
@guest = Guest.find(params[:id])
#Update the object
if @guest.update_attributes(guest_params)
#If the save succeeds, redirect to the show actions
redirect_to(guest_path(@guest))
else
#If the save fails, redisplay the form so user can fix problems
render('edit')
end
end
private
def guest_params
params.require(:guest).permit(:id, :guest_id, :first_name, :last_name)
end
end
我希望能够将嘉宾 ID 输入表格,单击更新按钮,并更新与该剧集相关的嘉宾。
看看这个并尝试一下。
http://railscasts.com/episodes/17-habtm-checkboxes-revised?autoplay=true
它应该可以解决您的问题。我还建议使用 has_many_through
而不是 has_and_belongs_to_many
,因为它更灵活。视频也谈到了这一点。
您可以按如下方式构建数据:
Guests
有许多 Appearances
到 Episodes
,反之 Episodes
到 Guests
。
这会让您查找 Guest.first.appearances
以查看史蒂夫出现在 3 集中,或者调用 Episode.third.guests
并查看史蒂夫、苏西和艾哈迈德都在该集中。
我正在为播客建立一个网站。我的 table 是 "episodes" 和 "guests" 以及一个名为 "episodes_guests" 的连接 table。 "episodes" 拥有并属于许多 "guests"。我有一个表格可以更新每集的信息,并且我试图让它也更新与该集相关的客人(数据存储在 join table 中),但无法弄清楚。
我已经能够添加一个 (form_for) 表单字段来填充相关的客人信息,但是当我点击更新时,这些字段不会更新连接 table(我什至没有确定我是否正在输出信息)。
这是来自剧集控制器 'edit' 视图的 table
<%= form_for(@episode) do |f| %>
<table summary="Episode form fields">
<tr>
<th> Episode ID </th>
<td><%= f.text_field(:id) %></td>
</tr>
<%= f.fields_for :guests, @episode.guests.each do |g| %>
<tr>
<!-- I am trying to add a selector for guests -->
<th>Guest: </th>
<td>
<%= g.text_area :last_name %>
</td>
</tr>
<% end %>
</table>
<div class="form-buttons">
<%= f.submit("Update Episode") %>
<% end %>
</div>
剧集模型
class Episode < ApplicationRecord
has_and_belongs_to_many :guests
accepts_nested_attributes_for :guests
scope:sorted, lambda {order("episode_number ASC")}
end
客座模特
class Guest < ApplicationRecord
has_and_belongs_to_many :episodes
scope:sorted, lambda {order("id ASC")}
end
剧集控制
class EpisodesController < ApplicationController
def edit
@episode = Episode.find(params[:id])
@guests = @episode.guests.all
end
def update
#Find a object using form parameters
@episode = Episode.find(params[:id])
@episode_guests = @episode.guests
#Update the object
if @episode.update_attributes(episode_params)
#If the save succeeds, redirect to the show actions
redirect_to(episode_path(@episode))
else
#If the save fails, redisplay the form so user can fix problems
render('edit')
end
end
private
def episode_params
params.require(:episode).permit(:id, :episode_number, :title, :guests, :file_name, :description, :date_released)
end
def guest_params
params.require(:guest).permit(:id, :guest_id, :first_name, :last_name)
end
end
来宾控制器
class GuestsController < ApplicationController
def edit
@guest = Guest.find(params[:id])
end
def update
#Find a object using form parameters
@guest = Guest.find(params[:id])
#Update the object
if @guest.update_attributes(guest_params)
#If the save succeeds, redirect to the show actions
redirect_to(guest_path(@guest))
else
#If the save fails, redisplay the form so user can fix problems
render('edit')
end
end
private
def guest_params
params.require(:guest).permit(:id, :guest_id, :first_name, :last_name)
end
end
我希望能够将嘉宾 ID 输入表格,单击更新按钮,并更新与该剧集相关的嘉宾。
看看这个并尝试一下。 http://railscasts.com/episodes/17-habtm-checkboxes-revised?autoplay=true
它应该可以解决您的问题。我还建议使用 has_many_through
而不是 has_and_belongs_to_many
,因为它更灵活。视频也谈到了这一点。
您可以按如下方式构建数据:
Guests
有许多 Appearances
到 Episodes
,反之 Episodes
到 Guests
。
这会让您查找 Guest.first.appearances
以查看史蒂夫出现在 3 集中,或者调用 Episode.third.guests
并查看史蒂夫、苏西和艾哈迈德都在该集中。