How do I use nested attributes with the devise model

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| %>
    

<%= company_form.label :name %>

<%= company_form.text_field :name %>

<% end %> ...

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

10
задан Community 23 May 2017 в 12:25
поделиться

1 ответ

Возможно, вы пытаетесь массово назначить некоторую защищенную переменную, ИЛИ вы не можете сохранить действительную запись. Убедитесь, что запись действительно сохраняется в базу данных.

2
ответ дан 3 December 2019 в 20:39
поделиться
Другие вопросы по тегам:

Похожие вопросы: