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

  • SEARCH
  • Home
  • 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 8718449
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T06:33:03+00:00 2026-06-13T06:33:03+00:00

I have two models: user and profile. I would like to use the same

  • 0

I have two models: user and profile. I would like to use the same form to input data to both at the same time. I was following railscast 196# about Nested Model Form (revised). The problem is, the first part of the form is being generated just fine. It’s the second part (where field_for is used), that does not show in the view.
I searched stackoverflow for a solution and it was suggested to use a ‘build’ action somehow. That, however, did not work, due to an error.

I would really appreciate it if someone could explain how I can make it work. I have been struggling with this issue for a few days now.

user.rb
class User < ActiveRecord::Base
attr_accessible :email, :first_name, :last_name, :orientation, :gender, :password, :password_confirmation, :date_joined, :last_online, :date_of_birth, :location, :profiles_attributes  
    has_one :profiles   
    accepts_nested_attributes_for :profiles, allow_destroy: true                    
    has_secure_password
    validates_confirmation_of :password
    #.....#
end

profile.rb
class Profile < ActiveRecord::Base
    attr_accessible :height, :weight
    belongs_to :user
end

user/new.html.erb
<%= form_for @user do |f| %>
    <% if @user.errors.any? %>
    <div class="error_messages">
      <h2>Form is invalid</h2>
      <ul>
        <% for message in @user.errors.full_messages %>
          <li><%= message %></li>
        <% end %>
      </ul>
    </div>
  <% end %>
        <%= f.label :gender, "I am a:" %><%= f.select :gender, options_for_select([["Man", "Male"], ["Woman", "Female"]]) %><br />
        <%= f.label :orientation, "Sexsual Orientation" %><%= f.select :orientation, options_for_select([["Straight", "Straight"], ["Gay", "Gay"], ["Bi","Bi"]]) %><br />
        <%= f.label :first_Name %><br /><%= f.text_field :first_name %><br />
        <%= f.label :last_name %><br /><%= f.text_field :last_name %><br />
        Date of Birth:<%= f.date_select( :date_of_birth, :start_year => 1920, :prompt => { :day => 'day', :month => 'month', :year => 'year' }) %><br />
        <%= f.label :location %><br /><%= f.text_field :location %><br />
        <%= f.label :email %><br /><%= f.text_field :email %><br />
        <%= f.label :password %><br /><%= f.password_field :password %><br />
            <%= f.label :password_confirmation %><br /><%= f.password_field :password_confirmation %><br />

        <%= f.fields_for :profiles do |builder| %>
          <fieldset>
            <%= builder.label :height, "My height is: (cm)" %><%= builder.text_field :height %><br />
            <%= builder.label :weight, "My weight is: (kg)" %><%= builder.text_field :weight %>
          </fieldset>
        <% end %>
    <%= f.submit "Next" %>
<% end %>

<%= link_to 'Back', users_path %>

edit:
new action in user_controller:
(as you can see it’s pretty standard)

  def new       
    @user = User.new


    respond_to do |format|
      format.html # new.html.erb
      format.json { render json: @user }
    end
  end
  • 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-06-13T06:33:04+00:00Added an answer on June 13, 2026 at 6:33 am

    I think you need

    has_one :profile
    

    in your user model.

    Make the form do

     fields_for :profile
    

    And in your users_controller do

    @user = User.new
    @user.build_profile
    

    has_one relationships work a little differently to has_many – see http://guides.rubyonrails.org/association_basics.html#has_one-association-reference

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I currently have two Backbone models: user and project. I would like to have
I have two models: User (email:string) Profile (name:string) class User < ActiveRecord::Base has_one :profile
I have two models: User and Car with the following associations: User has_many Car
I have two models User and Category that have a HABTM association. I would
I have two models User and Admin(with RailsAdmin) that use Devise. I sign in
I have the following two models: class Position(models.Model): position = models.CharField(max_length=100) class UserProfile(models.Model): user
I have two models user and profile. I want to save the username and
I have two models Profile and Avatar. models.py class Profile(models.Model): user = models.ForeignKey(User) profile_name
I have two models, User and Profile, in one-to-one relation and I am trying
I have two models -- User and Entry -- that are related through a

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.