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 4053230
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T14:25:15+00:00 2026-05-20T14:25:15+00:00

In my first rails app I’m trying to use form_for and fields_for to create

  • 0

In my first rails app I’m trying to use form_for and fields_for to create a nested object form. So far so good, but I can’t figure out how to access the sub-object while in the fields_for block. I’ve pre-populated a field in the sub-object with data that I want to show in the user instructions.

Models
Garage:

has_many :cars, :dependent => :destroy         
accepts_nested_attributes_for :cars

Car:

belongs_to :garage

Garage Controller

def new
  @garage = Garage.new
  for i in 1..5 
    @garage.cars.build :stall_number => i
  end
end

_form.html.erb

<%= form_for @garage do |f| %>
  <%= f.label :title, "Garage Name" %><br />
  <%= f.text_field :title %>
  <% f.fields_for :cars do |builder| %>
    <p>Enter license for car parked in stall: <%= car.stall_number %></p>
    <%= f.label :license, "License #:" %><br />
    <%= f.text_field :license %>
  <%= end %>
<%= end %>

As you can see, inside the builder block for :cars, I want to show, in my user instructions, the field: car.stall_number (populated in my controller with an integer):

<p>Enter license for car parked in stall: <%= car.stall_number%></p>

I’ve tried a many different ideas: @car.stall_number, object.car.stall_number, etc. No joy. Multiple searches and a look at the fields_for source code haven’t helped my understanding. I would appreciate any guidance.

Update: For clarification, per Dan’s suggestion I have tried builder.stall_number but it results in a

NoMethodError: undefined method 'stall_number' for #<ActionView::Helpers::FormBuilder:0x00000102a1baf0>
  • 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-20T14:25:15+00:00Added an answer on May 20, 2026 at 2:25 pm

    I just dealt with this today myself.

    You can access the object of the fields_for through:

    builder.object
    

    where builder is your fields_for form builder object. In your particular case, you can say:

    <p>Enter license for car parked in stall: <%= builder.object.stall_number%></p>
    

    That should do it for you!

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

Sidebar

Related Questions

I am planning to create an app in rails but first I want to
I've been building my first Rails app and now I'm trying to deploy it,
I'm trying to deploy my first rails app on a mediatemple (dv) and i'm
I'm a rails novice and just finished my first rails app(as far as I
Hey, I'm writing my first Rails app, and I'm trying to replace the underscores
I am making good progress with my first Rails app with a lot of
I've been trying to build my first rails app and have gotten stuck on
I am trying to deploy my first rails app and struggling a bit. My
I'm slowly but surely putting together my first rails app (first web-app of any
I'm building my first rails app and I'm trying to build the search page

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.