由于* _attributes后缀的形式,强参数,嵌套属性和Mongoid似乎不起作用? [英] Strong Params, Nested Attributes and Mongoid don't seem to work because of *_attributes suffix in form?

查看:79
本文介绍了由于* _attributes后缀的形式,强参数,嵌套属性和Mongoid似乎不起作用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图将属性组嵌入到我的主要Person模型中,以帮助保持代码井井有条,但是Rails 4的Strong Params妨碍了工作.

I'm trying to embed property groups on my main Person model to help keep my code organised but Rails 4's Strong Params is getting in the way.

我有一个Person模型和一个PersonName模型,如下所示:

I have a Person model and a PersonName model like so:

class Person
  include Mongoid::Document
  embeds_one :name, class_name: 'PersonName'
  accepts_nested_attributes_for :name
end

class PersonName
  include Mongoid::Document
  embedded_in :person, inverse_of: :name
  # fields here
end

然后我在表单中使用fields_for帮助器将PersonName属性嵌套在Person _form中:

I then use the fields_for helper in my form to nest the PersonName attributes in the Person _form:

<%= f.fields_for :name, PersonName.new do |builder| %>
  <!-- etc -->

最后,我允许在控制器中使用name属性,以便Strong Params通过以下方式允许嵌套属性:

and finally I permit the name attributes in the controller so that Strong Params allows the nested attributes through:

def person_params
  params.require(:person).permit(:name)
end

问题

它不起作用,并且出现错误Unpermitted parameters: name_attributes.发生了什么事,当我将f.fields_for :name ...称为"_attributes" get附加到HTML表单的字段中时.例如如果我有一个first_name字段,则表单如下所示:

The Problem

It doesn't work and I get the error Unpermitted parameters: name_attributes. What's happening is when I call f.fields_for :name ... a "_attributes" get's appended to the fields in the HTML form. e.g. If I have a field first_name then the form looks like this:

<input name="person[name_attributes][first_name] ...>

代替:

<input name="person[name][first_name] ...>

据我所知这是预期的行为,因此.permit方法似乎是事情无法正常工作的地方.

As far as I can tell this is intended behavior, so it seems like the .permit method is where things are failing to work.

我尝试将Strong Params行更改为:

I've tried changing the Strong Params line to:

params.require(:person).permit(:name_attributes)

但这是行不通的.使用permit! 可以,但是我不喜欢它,因为据我所知这是一个完整的反模式.

but this doesn't work. Using permit! does work, but I don't like it since as far as I can tell it's a complete anti-pattern.

除了在f.fields_for助手中,我还尝试将:name更改为:name_attributes,因为HTML表单现在具有模型中实际存在的属性.不幸的是,这导致f.fields_for :name产生错误,指出:name不是有效的财产.

I've also tried changing :name everywhere to :name_attributes, except in the f.fields_for helper, since the HTML form now had a property that actually existed on my model. Unfortunately this resulted in f.fields_for :name yielding an error saying :name is not a valid proterty.

推荐答案

Pierre-Louis Gottfrois是正确的,

Pierre-Louis Gottfrois, was right, this question fixes my problem. Specifically I needed to add:

params.require(:person).permit(name_attributes: [:first_name, ...])

,关键是name_attributes,后面是我要允许的符号列表.

with the key thing being name_attributes followed by a list of the symbols that I wanted to permit.

这篇关于由于* _attributes后缀的形式,强参数,嵌套属性和Mongoid似乎不起作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆