form_for 深度嵌套路由的多个实例
form_for multiple instances of deep nested routes
我正在为团队提供一种方式来参加体育比赛。进入球队时,用户还必须注册该球队的所有球员。我的协会和路线设置如下:
class Tournament < ApplicationRecord
has_many :teams
has_many :players, :through => :teams
accepts_nested_attributes_for :teams
end
class Team < ApplicationRecord
belongs_to :tournament
has_many :players
accepts_nested_attributes_for :players
end
class Player < ApplicationRecord
belongs_to :team
has_one :tournament, :through => :team
end
(routes.rb)
resources :tournaments do
resources :teams, :only => [:new, :create] do
resources :players, :only => [:new, :create]
end
end
我想要的是一种形式,其中包含多个玩家输入,只需单击一下即可全部保存。我现在的控制器&new.html.erb如下:
(players_controller.rb)
class PlayersController < ApplicationController
def create
@tournament = Tournament.find_by_id params[:tournament_id]
@team = Team.find_by_id params[:team_id]
@player = @team.players.new(player_params)
if @player.save
redirect_to root_path #just return home for now
else
redirect_to new_tournament_team_path(@tournament)
end
end
def new
@tournament = Tournament.find_by_id params[:tournament_id]
@team = Team.find_by_id params[:team_id]
@player = []
3.times do
@player << @team.players.new
end
end
private
def player_params
params.require(:player).permit(:name, :tournament_id, :team_id)
end
end
(players/new.html.erb)
<%= form_for [@tournament, @team, @player] do |f| %>
<% hidden_field_tag :tournament_id, @tournament.id %>
<% hidden_field_tag :team_id, @team.id %>
<% 3.times do %>
<p>
<%= f.label :name, "Name: " %>
<%= f.text_field :name %>
</p>
<% end %>
<%= submit_tag 'Submit', :class => 'rounded_btn' %>
</p>
<% end %>
根据我的理解,我应该尝试创建一个 "players" 数组,其中包含在表单中输入的 3 名玩家的姓名。然后这个数组被创建操作保存。这是正确的方法吗?我的代码中可能需要更改哪些内容才能让我走上正确的道路?
谢谢。
固定
应用了Ryan Bate's Nested Model Form tutorial
中的方法
removed validation for "belongs_to" 在 Rails 5.0
固定
应用了Ryan Bate's Nested Model Form tutorial
中的方法
removed validation for "belongs_to" 在 Rails 5.0
我正在为团队提供一种方式来参加体育比赛。进入球队时,用户还必须注册该球队的所有球员。我的协会和路线设置如下:
class Tournament < ApplicationRecord
has_many :teams
has_many :players, :through => :teams
accepts_nested_attributes_for :teams
end
class Team < ApplicationRecord
belongs_to :tournament
has_many :players
accepts_nested_attributes_for :players
end
class Player < ApplicationRecord
belongs_to :team
has_one :tournament, :through => :team
end
(routes.rb)
resources :tournaments do
resources :teams, :only => [:new, :create] do
resources :players, :only => [:new, :create]
end
end
我想要的是一种形式,其中包含多个玩家输入,只需单击一下即可全部保存。我现在的控制器&new.html.erb如下:
(players_controller.rb)
class PlayersController < ApplicationController
def create
@tournament = Tournament.find_by_id params[:tournament_id]
@team = Team.find_by_id params[:team_id]
@player = @team.players.new(player_params)
if @player.save
redirect_to root_path #just return home for now
else
redirect_to new_tournament_team_path(@tournament)
end
end
def new
@tournament = Tournament.find_by_id params[:tournament_id]
@team = Team.find_by_id params[:team_id]
@player = []
3.times do
@player << @team.players.new
end
end
private
def player_params
params.require(:player).permit(:name, :tournament_id, :team_id)
end
end
(players/new.html.erb)
<%= form_for [@tournament, @team, @player] do |f| %>
<% hidden_field_tag :tournament_id, @tournament.id %>
<% hidden_field_tag :team_id, @team.id %>
<% 3.times do %>
<p>
<%= f.label :name, "Name: " %>
<%= f.text_field :name %>
</p>
<% end %>
<%= submit_tag 'Submit', :class => 'rounded_btn' %>
</p>
<% end %>
根据我的理解,我应该尝试创建一个 "players" 数组,其中包含在表单中输入的 3 名玩家的姓名。然后这个数组被创建操作保存。这是正确的方法吗?我的代码中可能需要更改哪些内容才能让我走上正确的道路?
谢谢。
固定
应用了Ryan Bate's Nested Model Form tutorial
中的方法
removed validation for "belongs_to" 在 Rails 5.0
固定
应用了Ryan Bate's Nested Model Form tutorial
中的方法
removed validation for "belongs_to" 在 Rails 5.0