嵌套参数不包括<name>_attributes in Rails 7 提交嵌套属性时
Nested parameter does not include <name>_attributes in Rails 7 when submitting nested attributes
我正面临这个问题,经过深入研究,我似乎找不到解决方案。
我试图在嵌套表单上创建一个用户和他的 meet_infos,但在参数中,我总是以“meet_infos”而不是“meet_infos”结尾_属性":
Unpermitted parameters: :page_id, :meets_infos. Context: { controller: Users::MeetInfosController, action: create, request: #<ActionDispatch::Request:0x00007f224cc73128>, params: {"authenticity_token"=>"[FILTERED]", "user"=>{"page_id"=>"1", "last_name"=>"nxgfbngh", "first_name"=>"n hgn cgn", "phone"=>"nfcntrsbgxd", "email"=>"hbfdhnbytdgbn", "meets_infos"=>{"page_id"=>"1", "reason"=>"dfgnhgdn", "datetime(1i)"=>"2022", "datetime(2i)"=>"4", "datetime(3i)"=>"26", "datetime(4i)"=>"11", "datetime(5i)"=>"17"}}, "commit"=>"Save User", "controller"=>"users/meet_infos", "action"=>"create", "page_id"=>"1"} }
我的表格:
<%= simple_form_for :user do |f| %>
<%= f.error_notification %>
<%= f.input :page_id, as: :hidden, input_html: { value: @page.id } %>
<%= f.input :last_name %>
<%= f.input :first_name %>
<%= f.input :phone, :as => :tel %>
<%= f.input :email, :as => :email %>
<%= f.simple_fields_for :meet_infos do |mf| %>
<%= mf.input :page_id, as: :hidden, input_html: { value: @page.id } %>
<%= mf.input :reason, :as => :text %>
<%= mf.input :datetime, :as => :datetime %>
<% end %>
<%= f.submit %>
<% end %>
我的控制器:
class Users::MeetInfosController < ApplicationController
skip_before_action :authenticate_admin!
before_action :set_page, only: %i[index create destroy]
def index; end
def create
@user = User.new(user_params)
if @user.save
redirect_to page_users_meet_infos_path
flash[:success] = 'Rdv pris'
else
render :index
end
end
private
def user_params
params.require(:user).permit(:id, :last_name, :first_name, :phone, :email, { meet_infos_attributes: [:id, :reason, :datetime, :_destroy] }).merge(page_id: @page.id)
end
def set_page
@page = Page.find(params[:page_id])
end
end
用户模型:
class User < ApplicationRecord
belongs_to :page
has_many :meet_infos, dependent: :destroy
accepts_nested_attributes_for :meet_infos
validates_presence_of :page_id
validates :last_name, presence: true
validates :first_name, presence: true
validates :email, presence: true, uniqueness: true
validates :phone, presence: true, uniqueness: true
end
MeetInfo 模型:
class MeetInfo < ApplicationRecord
belongs_to :page
belongs_to :user
validates_presence_of :user
validates_presence_of :page_id
validates :reason, presence: true, uniqueness: true
validates :datetime, presence: true, uniqueness: true
end
你有一个偷偷摸摸的 nil 错误。使用:
<%= simple_form_for @user do |f| %>
用一个符号调用 form_for
和 simple_form_for
并让它解析一个实例变量是一个愚蠢的遗留特性,已经被贬低了很长时间,我想是时候砍掉它了.
举个简单的例子:
<%= simple_form_for(@country) do |form| %>
<% debugger form.object %>
<%= form.input :name %>
<fieldset>
<legend>Cities</legend>
<%= form.simple_fields_for :cities do |cities| %>
<%= cities.input :name %>
<% end %>
</fieldset>
<% end %>
这会将您传递给 simple_form_for
的模型生成给调试器。如果将其更改为符号 simple_form_for(:country)
,您将得到 nil 而不是该符号似乎仅用作 form_with
的范围参数-并且由于没有模型 fields_for
只是使用符号 :meet_infos
而不是调用对象上的方法。
可以说 simple_form_for
应该提出错误并警告该行为是贬低的。
我正面临这个问题,经过深入研究,我似乎找不到解决方案。
我试图在嵌套表单上创建一个用户和他的 meet_infos,但在参数中,我总是以“meet_infos”而不是“meet_infos”结尾_属性":
Unpermitted parameters: :page_id, :meets_infos. Context: { controller: Users::MeetInfosController, action: create, request: #<ActionDispatch::Request:0x00007f224cc73128>, params: {"authenticity_token"=>"[FILTERED]", "user"=>{"page_id"=>"1", "last_name"=>"nxgfbngh", "first_name"=>"n hgn cgn", "phone"=>"nfcntrsbgxd", "email"=>"hbfdhnbytdgbn", "meets_infos"=>{"page_id"=>"1", "reason"=>"dfgnhgdn", "datetime(1i)"=>"2022", "datetime(2i)"=>"4", "datetime(3i)"=>"26", "datetime(4i)"=>"11", "datetime(5i)"=>"17"}}, "commit"=>"Save User", "controller"=>"users/meet_infos", "action"=>"create", "page_id"=>"1"} }
我的表格:
<%= simple_form_for :user do |f| %>
<%= f.error_notification %>
<%= f.input :page_id, as: :hidden, input_html: { value: @page.id } %>
<%= f.input :last_name %>
<%= f.input :first_name %>
<%= f.input :phone, :as => :tel %>
<%= f.input :email, :as => :email %>
<%= f.simple_fields_for :meet_infos do |mf| %>
<%= mf.input :page_id, as: :hidden, input_html: { value: @page.id } %>
<%= mf.input :reason, :as => :text %>
<%= mf.input :datetime, :as => :datetime %>
<% end %>
<%= f.submit %>
<% end %>
我的控制器:
class Users::MeetInfosController < ApplicationController
skip_before_action :authenticate_admin!
before_action :set_page, only: %i[index create destroy]
def index; end
def create
@user = User.new(user_params)
if @user.save
redirect_to page_users_meet_infos_path
flash[:success] = 'Rdv pris'
else
render :index
end
end
private
def user_params
params.require(:user).permit(:id, :last_name, :first_name, :phone, :email, { meet_infos_attributes: [:id, :reason, :datetime, :_destroy] }).merge(page_id: @page.id)
end
def set_page
@page = Page.find(params[:page_id])
end
end
用户模型:
class User < ApplicationRecord
belongs_to :page
has_many :meet_infos, dependent: :destroy
accepts_nested_attributes_for :meet_infos
validates_presence_of :page_id
validates :last_name, presence: true
validates :first_name, presence: true
validates :email, presence: true, uniqueness: true
validates :phone, presence: true, uniqueness: true
end
MeetInfo 模型:
class MeetInfo < ApplicationRecord
belongs_to :page
belongs_to :user
validates_presence_of :user
validates_presence_of :page_id
validates :reason, presence: true, uniqueness: true
validates :datetime, presence: true, uniqueness: true
end
你有一个偷偷摸摸的 nil 错误。使用:
<%= simple_form_for @user do |f| %>
用一个符号调用 form_for
和 simple_form_for
并让它解析一个实例变量是一个愚蠢的遗留特性,已经被贬低了很长时间,我想是时候砍掉它了.
举个简单的例子:
<%= simple_form_for(@country) do |form| %>
<% debugger form.object %>
<%= form.input :name %>
<fieldset>
<legend>Cities</legend>
<%= form.simple_fields_for :cities do |cities| %>
<%= cities.input :name %>
<% end %>
</fieldset>
<% end %>
这会将您传递给 simple_form_for
的模型生成给调试器。如果将其更改为符号 simple_form_for(:country)
,您将得到 nil 而不是该符号似乎仅用作 form_with
的范围参数-并且由于没有模型 fields_for
只是使用符号 :meet_infos
而不是调用对象上的方法。
可以说 simple_form_for
应该提出错误并警告该行为是贬低的。