0

I'm struggling with Rails syntax. I'm trying to set up a one-to-many relationship for users to addresses, but the user will always have a primary address. I've managed to get the model working, but I can'seem to get the form working. The form below is for "new registration", but the fields for the nested address don't show up.

I've been hacking away at this, and can't seem to get it. Could someone please explain why the nested form fields wouldn't show up when trying to register a New user?

This is the form

 <%= form_for(resource, :as => resource_name, :url => registration_path(resource_name), :html => {:class => "form-horizontal"}) do |f| %>
    <small>Mandatory fields marked *</small><br><br>


    <%= f.fields_for :primary_address do |address_form| %>
    <div class="control-group">
        <%= address_form.label :first_name, :class => "control-label" %>
        <div class="controls">
          <%= address_form.text_field :first_name, :class => "input-xlarge" %>
        </div>
      </div>

    <div class="control-group">
        <%= address_form.label :last_name, :class => "control-label" %>
        <div class="controls">
          <%= address_form.text_field :last_name, :class => "input-xlarge" %>
        </div>
      </div>
    <% end %>

This is the user model:

class Member::User < ActiveRecord::Base
  # Include default devise modules. Others available are:
  # :token_authenticatable, :confirmable,
  # :lockable, :timeoutable and :omniauthable
  devise :database_authenticatable, :registerable,
  :recoverable, :rememberable, :trackable, :validatable


  # Setup accessible (or protected) attributes for your model
  attr_accessible :email, :password, :password_confirmation, :remember_me, :addresses_attributes

  validates_presence_of :email

  # each user may have zero or many addresses
  # their primary email is also set as their login id.
  has_many :addresses

  has_one :primary_address, :class_name => "Address",  :conditions => "is_primary = true"

  accepts_nested_attributes_for :primary_address

end
2
  • Aren't you missing an <% end %> on the bottom of the form? Commented Sep 16, 2012 at 13:33
  • I didn't post the whole form...just the relevant part. Commented Sep 17, 2012 at 14:03

1 Answer 1

1

try to initialize a new primary_address like this:

<%= f.fields_for resource.build_primary_address do |address_form| %>
Sign up to request clarification or add additional context in comments.

5 Comments

That's a good way to do it. Just watch out if you want to re-use this form for both your new and edit actions. In that case, this way may change an existing, saved address (during edit).
Thnx for the refinement max. I'd also add to it the case in which resource is not a User instance and may not have a primary_addess association.
Ok thanks that works. Can I hijack the question to try to help the next problem: Saving now throws this error "Can't mass-assign protected attributes: member_address". Could you check my attr_accessible statement too please? I'm not sure how to specify this for nested model. Thanks for the help.
I think it's getting caught up on the fact that you've specified attr_accessible :address_attributes, but your nested attributes writer is called primary_address_attributes. I'd rename one or the other so they're called the same thing.
Thanks Max, I tried a few variations and it didn't work. I think I"ll accept this question and open another question. Thanks again.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.