Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7696259
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T21:44:17+00:00 2026-05-31T21:44:17+00:00

I have 2 models User and Address . class User < ActiveRecord::Base has_many :addresses

  • 0

I have 2 models User and Address .

class User < ActiveRecord::Base 
  has_many :addresses
  accepts_nested_attributes_for :addresses
end

class Address < ActiveRecord::Base
  belongs_to :user
end

My controller

  def new
    @user = User.new
    @user.addresses << Address.new
    @user.addresses << Address.new
  end

  def create
    @user = User.new(params[:user])
    if @user.save
      #do something
    else
      render 'new'
    end
  end

And my View

 <%= form_for @user do |f| %>
     <%= f.label :name %>
     <%= f.text_field :name %>
       <%= f.fields_for :addresses do |a| %>
         <p> Home </p>
         <%= a.text_field :state %>
         <%= a.text_field :country%>
         <%= a.text_field :street %>
         <p> Work </p>
         <%= a.text_field :state %>
         <%= a.text_field :country%>
         <%= a.text_field :street %>
       <% end %>
     <% end %>
   <% end %>

My problem is I get only the last state,country,street entered in params .

"addresses_attributes"=>{"0"=>{"street"=>"test", "state"=>"test",, "country"=>"test"},
"1"=>{"street"=>"", "state"=>"", "country"=>""}} 

Also if there’s a better way to do this I will appreciate any suggestions .

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T21:44:19+00:00Added an answer on May 31, 2026 at 9:44 pm

    The rails API says that fields_for will be repeated by it self over each element in the collection of addresses.

    I would suggest adding a kind of label to your addresses (like Work, Home, etc). And then it should work by itself. And with this label your are a bit more flexible when you want to add more addresses.

       <%= f.fields_for :addresses, @user.addresses do |a| %>
         <p> <%= a.object.label %> </p>
         <%= a.text_field :state %>
         <%= a.text_field :country%>
         <%= a.text_field :street %>
       <% end %>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have 3 models class User < ... belongs_to :language has_many :posts end class
I have two models: class User end class Message belongs_to :sender, :class_name=> 'User' belongs_to
I have this model: class Location < ActiveRecord::Base acts_as_gmappable def gmaps4rails_address #{self.city}, #{self.country} end
I have 3 models sites, user_favorites and users. Relevant relationships: class Site < ActiveRecord::Base
I have following model: class Client(models.Model): user = models.OneToOneField(DjangoUser, unique=True) address = models.ForeignKey(Address,blank=True) class
I have some nested attributes in some models as such: class Employee < ActiveRecord::Base
Suppose I have the following models: class User(models.Model): pass class A(models.Model): user = models.ForeignKey(User)
I have a Class which models a User and another which models his country.
I have such model: class Place(models.Model): name = models.CharField(max_length=80, db_index=True) city = models.ForeignKey(City) address
Updated I have the following custom field class: class Binary(models.Field): __metaclass__ = models.SubfieldBase def

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.