如何在设计模型中使用嵌套属性 [英] How do I use nested attributes with the devise model
问题描述
我有与 使用 Devise 创建其他相关模型的相同问题(没有答案).
I have the same issue as Creating an additional related model with Devise (which has no answer).
我已经覆盖了用于创建新用户的设计视图并添加了公司名称,我已将模型更改为使用 accepts_nested_attributes_for
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 %>
...
更新:我没有将 :company 添加到 User 模型中的 attr_accessible 列表中.
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屋!