ruby-on-rails 关系fields_for上不允许的参数

0dxa2lsx  于 2023-10-21  发布在  Ruby
关注(0)|答案(1)|浏览(122)

我正在学习Ruby on Rails的表单助手和阅读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"} }

和泽西岛的关系并没有持续下去。我在Acer硝基AN 515 -55上的Ubuntu 22.04.3 LTS上使用Rails 7.0.8和Ruby 3.2.1
这是一个简单的例子。模型是:玩家拥有_多件球衣

class Player < ApplicationRecord
      has_many :jerseys, dependent: :destroy
      accepts_nested_attributes_for :jerseys
    end
    class Jersey < ApplicationRecord
      belongs_to :player
    end

players_controller(仅index、new、create和player_params)

# 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

我在index.htl.erb中有表格

<%= 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| %>

但结果是一样的...在这一点上,我不知道是什么错了,这是一个简单的例子,但我不能使它工作,请帮助我

68bkxrlz

68bkxrlz1#

accepts_nested_attributes_for :jerseys表示:

#                      v
form.fields_for :jerseys do |jersey_form|

通过这种方式,表单生成器可以检测jerseys关联并生成正确的jerseys_attributes输入名称。

更新

不如我们从头开始:

bin/rails g scaffold Player name number
bin/rails g model Jersey size player:references
bin/rails db:migrate
# app/models/jersey.rb
class Jersey < ApplicationRecord
  belongs_to :player
end

# app/models/player.rb
class Player < ApplicationRecord
  has_many :jerseys, dependent: :destroy
  accepts_nested_attributes_for :jerseys
end
# app/controllers/players_controller.rb

class PlayersController < ApplicationController
  def new
    @player = Player.new
    @player.jerseys.new # build a nested object
  end

  def create
    @player = Player.new(player_params)
    respond_to do |format|
      if @player.save
        format.html { redirect_to player_url(@player), notice: "Created." }
      else
        format.html { render :new, status: :unprocessable_entity }
      end
    end
  end

  private

  def player_params
    params.require(:player).permit(:name, :number, jerseys_attributes: [:id, :size])
  end
end
# app/views/players/_form.html.erb

<%= form_with model: player do |form| %>
  <%= form.label :name %>
  <%= form.text_field :name %>

  <%= form.label :number %>
  <%= form.text_field :number %>

  <%= form.fields_for :jerseys do |jersey_form| %>
    <%= jersey_form.label :size %>
    <%= jersey_form.text_field :size %>
  <% end %>

  <%= form.submit %> 
<% end %>

填写并提交:

Started POST "/players" for 127.0.0.1 at 2023-09-16 22:42:06 -0400
Processing by PlayersController#create as TURBO_STREAM
  Parameters: {"authenticity_token"=>"[FILTERED]", "player"=>{"name"=>"asdf", "number"=>"1", "jerseys_attributes"=>{"0"=>{"size"=>"xl"}}}, "commit"=>"Create Player"}
  TRANSACTION (0.1ms)  begin transaction
  ↳ app/controllers/players_controller.rb:10:in `block in create'
  Player Create (0.2ms)  INSERT INTO "players" ("name", "number", "created_at", "updated_at") VALUES (?, ?, ?, ?)  [["name", "asdf"], ["number", "1"], ["created_at", "2023-09-17 02:42:06.071928"], ["updated_at", "2023-09-17 02:42:06.071928"]]
  ↳ app/controllers/players_controller.rb:10:in `block in create'
  Jersey Create (0.1ms)  INSERT INTO "jerseys" ("size", "player_id", "created_at", "updated_at") VALUES (?, ?, ?, ?)  [["size", "xl"], ["player_id", 1], ["created_at", "2023-09-17 02:42:06.074850"], ["updated_at", "2023-09-17 02:42:06.074850"]]
  ↳ app/controllers/players_controller.rb:10:in `block in create'
  TRANSACTION (7.3ms)  commit transaction
# PERSISTED            ^^^^^^
  ↳ app/controllers/players_controller.rb:10:in `block in create'
Redirected to http://0.0.0.0:3000/players/1

索引操作的工作原理是一样的:

def index
  @players = Player.all

  @player = Player.new
  @player.jerseys.new
end
# app/views/players/index.html.erb

<%= render "form", player: @player %>

相关问题