nested one-to-one forms question

Hello, I must admit I'm new to rails (I actually come from the .net
world, but I've been meaning to expand for some time now)

I'm trying to create a user registration page (just getting my feet wet
really) and I've hit a wall. I'm pretty sure I'm doing this wrong, so I
need some straightening out :slight_smile:

My db looks something like this:

UserAccount: Name, Email, Website, Status
Password: Key, Question, Answer

and my models:

class UserAccount < ActiveRecord::Base
  validates_presence_of :Name, :Email
  validates_uniqueness_of :Name, :Email

  has_one :Password

  accepts_nested_attributes_for :Password
end

class Password < ActiveRecord::Base
  belongs_to :user_account

  attr_accessor :password_confirmation
  validates_confirmation_of :password
end

and the form that I'm trying to produce:

<% form_for(@user_account) do |f| %>
  <%= f.error_messages %>

  <fieldset>
    <legend>Required</legend>

    <p>
      <%= f.label :Name %><br />
      <%= f.text_field :Name %>
    </p>

    <% f.fields_for :Password do |fp| %>
      <p>
        <%= fp.label :Password %><br />
        <%= fp.password_field :key %>
      </p>
      <p>
        <%= fp.label :password_confirmation %><br />
        <%= fp.password_field :password_confirmation %>
      </p>
    <% end %>
    <p>
      <%= f.label :Email %><br />
      <%= f.text_field :Email %>
    </p>
  </fieldset>
  <fieldset>
    <legend>Optional</legend>
    <p>
      <%= f.label :Website %><br />
      <%= f.text_field :Website %>
    </p>
    <p>
      <%= f.label :Status %><br />
      <%= f.text_area :Status %>
    </p>

  </fieldset>
  <p>
    <%= f.submit 'Create' %>
  </p>
<% end %>

Now, as is, the browser doesn't render the password at all. I think
that it is because "fields_for" is expecting a one-to-many relationship
and finding a one-to-one relationship (?), but if I take out the line
accepts_nested_attributes_for :Password I get the following error:

ActiveRecord::AssociationTypeMismatch in User accountsController#create
Password(#57105720) expected, got HashWithIndifferentAccess(#30298510)

I'm really stuck and I probably look really dumb, so that's why i'm here
:slight_smile:

Thank you for ANY advice and sorry for my poor design/implementation

whoa, I guess I better explain what I'm trying to do.

What I want to do is be able to create a User and a Password object with
the same form. I'm going to add the question part later (once this is
done)

I figure that since User has a single instance of a Password, I could
gain access to it just like an attribute, and render the text_fields
with :Password.Key or :Password.Question, but that doesn't work either.

So if I can be shown how to edit 2 models (or just a model and its sub
model) with the same form, that would be excellent

Thanks!

Joel Day wrote:

whoa, I guess I better explain what I'm trying to do.

What I want to do is be able to create a User and a Password object with
the same form. I'm going to add the question part later (once this is
done)

I figure that since User has a single instance of a Password, I could
gain access to it just like an attribute, and render the text_fields
with :Password.Key or :Password.Question, but that doesn't work either.

So if I can be shown how to edit 2 models (or just a model and its sub
model) with the same form, that would be excellent

Thanks!

Basically in your controller have the new action create 2 new instances:

@user_account = User.new
@user_account.password.build

form_new needs these new instances, specially the password one. That is
why its not rendering it.