如何使用嵌套属性与设计模型 [英] How do I use nested attributes with the devise model

查看:114
本文介绍了如何使用嵌套属性与设计模型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有与使用Devise创建额外的相关模型

相同的问题a>(没有答案)。



我已经覆盖了创建新用户的设计视图,并添加了公司名称,我已经将模型更改为use_nested_attributes_for



没有错误,但是不添加嵌套记录,我没有可以修改请求的控制器。 / p>

我有以下(缩短以使其可读):



routes.rb

  map.devise_for:users 
map.resources:users,:has_many => :公司

user.rb

  has_many:companies 
accepted_nested_attributes_for:companies
设计:可注册...等

company.rb

  belongs_to :user 

new.html.erb

  ... 
<%form_for resource_name,resource,:url => registration_path(resource_name)do | f | %GT;
...
<%f.fields_for:company do | company_form | %GT;
< p><%= company_form.label:name%>< / p>
< p><%= company_form.text_field:name%>< / p>
<%end%>
...

更新:
我没有添加:公司用户模型中的attr_accessible列表。

解决方案

您可能正在尝试大量分配一些受保护的变量,或者您可能无法保存有效的记录。检查以确保记录实际上保存到db。


I have the same issue as Creating an additional related model with Devise (which has no answer).

I have overridden the devise view for creating a new user and added a company name, I have changed the model to use accepts_nested_attributes_for

There are no errors, but it is not adding the nested record and I don't have a controller where I can modify the request.

I have the following (shortened to make it readable):

routes.rb

map.devise_for :users
map.resources :users, :has_many => :companies

user.rb

has_many :companies
accepts_nested_attributes_for :companies
devise :registerable ... etc

company.rb

belongs_to :user

new.html.erb

...
<% form_for resource_name, resource, :url => registration_path(resource_name) do |f| %>
...
  <% f.fields_for :company do |company_form| %>
    <p><%= company_form.label :name %></p>
    <p><%= company_form.text_field :name %></p>
  <% end %>
...

UPDATE: I didn't add :company to the attr_accessible list in the User model.

解决方案

You may be trying to mass assign some protected variable, OR you might not be saving a valid record. Check to make sure that the record is actually saving to the db.

这篇关于如何使用嵌套属性与设计模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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