forms - Rails - Using form_for and fields_for, how do you access the sub-object while in the fields_for block? -
in first rails app i'm trying use form_for , fields_for create nested object form. far good, can't figure out how access sub-object while in fields_for block. i've pre-populated field in sub-object data want show in 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 in 1..5 @garage.cars.build :stall_number => end end _form.html.erb
<%= form_for @garage |f| %> <%= f.label :title, "garage name" %><br /> <%= f.text_field :title %> <% f.fields_for :cars |builder| %> <p>enter license car parked in stall: <%= car.stall_number %></p> <%= f.label :license, "license #:" %><br /> <%= f.text_field :license %> <%= end %> <%= end %> as can see, inside builder block :cars, want show, in user instructions, field: car.stall_number (populated in controller integer):
<p>enter license car parked in stall: <%= car.stall_number%></p> i've tried many different ideas: @car.stall_number, object.car.stall_number, etc. no joy. multiple searches , @ fields_for source code haven't helped understanding. appreciate guidance.
update: clarification, per dan's suggestion have tried builder.stall_number results in a
nomethoderror: undefined method 'stall_number' #<actionview::helpers::formbuilder:0x00000102a1baf0>
i dealt today myself.
you can access object of fields_for through:
builder.object where builder fields_for form builder object. in particular case, can say:
<p>enter license car parked in stall: <%= builder.object.stall_number%></p> that should you!
Comments
Post a Comment