Rails: 嵌套表单未创建记录
Rails: Nested form not creating record
我正在尝试构建一个表单来创建一个具有四个不同玩家实例的游戏。我构建了一个嵌套表单,将四组玩家字段呈现给游戏表单。但是,当我提交表单时没有任何反应。我想提交表单以调用我的控制器的 #create 操作,但它甚至不调用创建(我尝试在 #create 中引发错误但不能)。
这是我的代码:
game.rb
class Game < ApplicationRecord
has_many :players, dependent: :destroy
has_many :rounds, dependent: :destroy
accepts_nested_attributes_for :players
end
player.rb
class Player < ApplicationRecord
belongs_to :game
has_many :rounds
validates :name, presence: true, length: { maximum: 15 }
end
games_controller.rb
class GamesController < ApplicationController
def create
raise
@new_game = Game.new(game_params)
@new_game.start = Date.today
@new_game.save!
redirect_to root_url
end
def new
@game = Game.new
4.times { @game.players.build }
end
private
def set_game
@game = Game.find(params[:id])
end
def game_params
params.require(:game).permit(:date, player_attributes: [:name])
end
end
views/games/new.rb
<%= form_with model: @game do |f| %>
Players:
<ul>
<%= f.fields_for :players do |players_form| %>
<li>
<%= players_form.label :name %>
<%= players_form.text_field :name %>
</li>
<% end %>
<%= f.submit "Submit"%>
</ul>
<% end %>
routes.rb
Rails.application.routes.draw do
root to: 'pages#home'
resources :games, only: [:create, :new]
end
这是我提交表单时的日志:
Started POST "/games" for ::1 at 2020-01-15 12:54:13 +0100
Processing by GamesController#create as JS
Parameters: {"utf8"=>"✓", "authenticity_token"=>"owiE4RViIsVonr9dwDAlLZR8MtZ/EnY/VTUq7o5zxHc2KcQ0Nliag9XnDHQZ7xKc5cIqOwQbuoPwSgkWPMknSA==", "game"=>{"players_attributes"=>{"0"=>{"name"=>"Bob"}, "1"=>{"name"=>"Joe"}, "2"=>{"name"=>"Alice"}, "3"=>{"name"=>"Fernand"}}}, "commit"=>"Submit"}
Completed 500 Internal Server Error in 7ms (ActiveRecord: 0.0ms)
RuntimeError ():
app/controllers/games_controller.rb:4:in `create'
这是我第一次在 Whosebug 上提问,如果我不清楚,请告诉我enough/need 以提供更多信息。
谢谢!
问题是player_attributes
应该是
def family_params
params.require(:game).permit(:date, players_attributes: [:name])
end
根据您的日志,您的表单已作为 JS 提交。
只需将 local 设置为 true 即可禁用此功能:
views/games/new.rb
<%= form_with model: @game, local: true do |f| %>
参考:form_with
我正在尝试构建一个表单来创建一个具有四个不同玩家实例的游戏。我构建了一个嵌套表单,将四组玩家字段呈现给游戏表单。但是,当我提交表单时没有任何反应。我想提交表单以调用我的控制器的 #create 操作,但它甚至不调用创建(我尝试在 #create 中引发错误但不能)。
这是我的代码:
game.rb
class Game < ApplicationRecord
has_many :players, dependent: :destroy
has_many :rounds, dependent: :destroy
accepts_nested_attributes_for :players
end
player.rb
class Player < ApplicationRecord
belongs_to :game
has_many :rounds
validates :name, presence: true, length: { maximum: 15 }
end
games_controller.rb
class GamesController < ApplicationController
def create
raise
@new_game = Game.new(game_params)
@new_game.start = Date.today
@new_game.save!
redirect_to root_url
end
def new
@game = Game.new
4.times { @game.players.build }
end
private
def set_game
@game = Game.find(params[:id])
end
def game_params
params.require(:game).permit(:date, player_attributes: [:name])
end
end
views/games/new.rb
<%= form_with model: @game do |f| %>
Players:
<ul>
<%= f.fields_for :players do |players_form| %>
<li>
<%= players_form.label :name %>
<%= players_form.text_field :name %>
</li>
<% end %>
<%= f.submit "Submit"%>
</ul>
<% end %>
routes.rb
Rails.application.routes.draw do
root to: 'pages#home'
resources :games, only: [:create, :new]
end
这是我提交表单时的日志:
Started POST "/games" for ::1 at 2020-01-15 12:54:13 +0100
Processing by GamesController#create as JS
Parameters: {"utf8"=>"✓", "authenticity_token"=>"owiE4RViIsVonr9dwDAlLZR8MtZ/EnY/VTUq7o5zxHc2KcQ0Nliag9XnDHQZ7xKc5cIqOwQbuoPwSgkWPMknSA==", "game"=>{"players_attributes"=>{"0"=>{"name"=>"Bob"}, "1"=>{"name"=>"Joe"}, "2"=>{"name"=>"Alice"}, "3"=>{"name"=>"Fernand"}}}, "commit"=>"Submit"}
Completed 500 Internal Server Error in 7ms (ActiveRecord: 0.0ms)
RuntimeError ():
app/controllers/games_controller.rb:4:in `create'
这是我第一次在 Whosebug 上提问,如果我不清楚,请告诉我enough/need 以提供更多信息。
谢谢!
问题是player_attributes
应该是
def family_params
params.require(:game).permit(:date, players_attributes: [:name])
end
根据您的日志,您的表单已作为 JS 提交。
只需将 local 设置为 true 即可禁用此功能:
views/games/new.rb
<%= form_with model: @game, local: true do |f| %>
参考:form_with