提交嵌套属性时,嵌套参数不包括< name> _attributes 7
我面对这个问题,挖掘后,我似乎找不到解决方案。
我正在尝试在嵌套表单上创建一个用户和他的MET_INFOS,但是在参数中,我总是以“ Meet_infos”而不是“ Meet_infos_attributes”:
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
控制器:用户模型:MeetInfo Model:
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 Model:
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
I'm facing this issue and after digging, I can't seem to find a solution for this.
I'm trying to create a user and his meet_infos on a nested form, but in the params, I always end up with "meet_infos" instead of "meet_infos_attributes":
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"} }
My form:
<%= 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 %>
My controller:
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
User model:
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 model:
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您有一个偷偷摸摸的零错误。使用:
调用
form_for
和simple_form_for
带有符号并使其解析一个实例变量是一个愚蠢的遗产功能,已剥夺了很长时间,我猜是ax的时间终于来了。以这个简单的示例:
这将使您已将您传递给
simple_form_for
的模型发送给调试器。如果将其更改为符号simple_form_for(:country)
您将获得零,并且该符号似乎只是用作form_with
的范围参数 - 不是模型fields_for
只是使用符号:meet_infos
而不是在对象上调用方法。可以说,
simple_form_for
应该提出错误,并警告该行为被贬低。You have a sneaky nil error. Use:
Calling
form_for
andsimple_form_for
with a symbol and having it resolve an instance variable was a stupid legacy feature that has been depreachiated for a long time and I guess the time to axe it finally came.Take this simple example:
This will yield the model that you have passed to
simple_form_for
to the debugger. If you change it to a symbolsimple_form_for(:country)
you'll get nil instead and the symbol seems to just be used as the scope argument forform_with
- and since there is no modelfields_for
is just using the symbol:meet_infos
instead of calling the method on the object.It could be argued that
simple_form_for
instead should have raised an error and warned that the behavior is deprechiated.