English 中文(简体)
关系领域未经许可的参数
原标题:Unpermitted parameter on relationship fields_for

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

但它也是同样的结果...... 现在我不知道什么是错的,这是一个简单的例子,但我无法工作。

问题回答

accepts_nested_attributes_for:jerseys, 这意味着:

#                      v
form.fields_for :jerseys do |jersey_form|

This way form builder can detect jerseys association and generate correct jerseys_attributes input names.


Update

How about we start from scratch:

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




相关问题
rails collection_select vs. select

collection_select and select Rails helpers: Which one should I use? I can t see a difference in both ways. Both helpers take a collection and generates options tags inside a select tag. Is there a ...

SSL slowness in EC2

We ve deployed our rails app to EC2. In our setup, we have two proxies on small instances behind round-robin DNS. These run nginx load balancers for a dynamically growing and shrinking farm of web ...

Auth-code with A-Za-z0-9 to use in an URL parameter

As part of a web application I need an auth-code to pass as a URL parameter. I am currently using (in Rails) : Digest::SHA1.hexdigest((object_id + rand(255)).to_s) Which provides long strings like : ...

RubyCAS-Client question: Rails

I ve installed RubyCAS-Client version 2.1.0 as a plugin within a rails app. It s working, but I d like to remove the ?ticket= in the url. Is this possible?

activerecord has_many :through find with one sql call

I have a these 3 models: class User < ActiveRecord::Base has_many :permissions, :dependent => :destroy has_many :roles, :through => :permissions end class Permission < ActiveRecord::...

Ordering a hash to xml: Rails

I m building an xml document from a hash. The xml attributes need to be in order. How can this be accomplished? hash.to_xml

Text Editor for Ruby-on-Rails

guys which text editor is good for Rubyonrails? i m using Windows and i was using E-Texteditor but its not free n its expired now can anyone plese tell me any free texteditor? n which one is best an ...

How to get SQL queries for each user where env is production

I’m developing an application dedicated to generate statistical reports, I would like that user after saving their stat report they save sql queries too. To do that I wrote the following module: ...

热门标签