I m Learning Duncan on Railways form helpers and Read the https://guides.rubyonrails.org/form_helpers.html 文件 我试图用我的电脑进行操练,但正如文件所描述的那样,这只是打赢了不起的工作。
Started POST "/players" for ::1 at 2023-09-15 22:48:45 -0600
Processing by PlayersController#create as TURBO_STREAM
Parameters: {"authenticity_token"=>"[FILTERED]", "player"=>{"name"=>"Luis", "number"=>"45", "jersey"=>{"size"=>"M"}}, "commit"=>"Create Player"}
Unpermitted parameter: :jersey. Context: { controller: PlayersController, action: create, request: #<ActionDispatch::Request:0x00007f117a4a0640>, params: {"authenticity_token"=>" [FILTERED]", "player"=>{"name"=>"Luis", "number"=>"45", "jersey"=>{"size"=>"M"}}, "commit"=>"Create Player", "controller"=>"players", "action"=>"create"} }
And the Jersey relationship is not persisted. I m using Rails 7.0.8 and ruby 3.2.1 on an Ubuntu 22.04.3 LTS on a Acer Nitro AN515-55
This is the code involved: Models are: Player Has_many Jersey
class Player < ApplicationRecord
has_many :jerseys, dependent: :destroy
accepts_nested_attributes_for :jerseys
end
class Jersey < ApplicationRecord
belongs_to :player
end
参与者——控制者(仅指数、新指数、创造力和角色)
# GET /players or /players.json
def index
@players = Player.all
@player = Player.new
end
# GET /players/new
def new
@player = Player.new
end
# POST /players or /players.json
def create
@player = Player.new(player_params)
respond_to do |format|
if @player.save
format.html { redirect_to players_url, notice: "Player was successfully created." }
format.json { render :show, status: :created, location: @player }
else
format.html { render :new, status: :unprocessable_entity }
format.json { render json: @player.errors, status: :unprocessable_entity }
end
end
end
# Only allow a list of trusted parameters through.
def player_params
params.require(:player).permit(:name, :number, jerseys_attributes: [:id, :size])
end
我的表格载于指数。
<%= form_with(model: @player) do |form| %>
<div>
<%= form.label :name, style: "display: block" %>
<%= form.text_field :name %>
</div>
<div>
<%= form.label :number, style: "display: block" %>
<%= form.number_field :number %>
</div>
<div>
<%= form.fields_for :jersey do |jersey_form| %>
<%= jersey_form.label :size, style: "display: block" %>
<%= jersey_form.text_field :size %>
<% end %>
</div>
<div>
<%= form.submit %>
</div>
<% end %>
<div id="players">
<% @players.each do |player| %>
<%= render player %>
<p>
<%= link_to "Show this player", player %>
</p>
<% end %>
</div>
<%= link_to "New player", new_player_path %>
我还努力:
<%= form.fields_for :jersey, @player.jerseys do |jersey_form| %>
但它也是同样的结果...... 现在我不知道什么是错的,这是一个简单的例子,但我无法工作。