Вопрос

I have a User model with has_one relation to a Profile model. The form generates correctly (I see the additional field for contact_person), but upon creating a new record only the user is saved. What I am missing?

class User < ActiveRecord::Base
  devise :database_authenticatable, :registerable,
     :recoverable, :rememberable, :trackable, :validatable

  has_one :profile
  accepts_nested_attributes_for :profile
end

and

class Profile < ActiveRecord::Base
  belongs_to :user
end

This is my form:

<% @user.build_profile if @user.profile.nil? %>
<%= f.fields_for :profile do |profile| %>
  <%= profile.label :contact_person %>
  <%= profile.text_field :contact_person %>
<% end  %>
Это было полезно?

Решение 2

So, I found this info on using Devise and strong parameters from the Wiki which solved my problem.

Другие советы

You have to add attr_accessible :profile_attributes in User model.

  class User < ActiveRecord::Base

    attr_accessible :profile_attributes

      devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable

      has_one :profile
      accepts_nested_attributes_for :profile
    end
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top