fields for - Rails 4 and fields_for using seed data. How to pre-populate form? -
the goal pre-populate form seed data: https://github.com/ornerymoose/garageapp
the relevant code iterating on stall numbers in garages_controller.rb
:
def new @garage = garage.new in 1..5 @garage.cars.build :stall_number => end end
this give 5 stall_number string values of 1, 2, 3, 4, , 5. instead of that, how iterate on values in seeds.rb
file?
car.create(stall_number: "stall1") car.create(stall_number: "stall2") car.create(stall_number: "stall3") car.create(stall_number: "stall4") car.create(stall_number: "stall5")
so instead of stall_numbers having values of 1, 2, 3, 4, , 5. have string values listed in seeds.rb file.
here fields_for
block in garage _form.html.erb
:
<%= f.fields_for :cars |builder| %> <p>enter license car parked in stall: <%= builder.object.stall_number %></p> <%= builder.label :license, "license #:" %><br /> <%= builder.text_field :license %> <% end %>
any input on matter appreciated.
just same loop in seed file.
for in 1..5 car.create(stall_number: "stall" + i.to_s) end
if static, create them in array , call them.
array = ["stalla", "stalldc", "stall874", "stallnn", "stallpo", "stalsf", "stallre", "stall456", "stall39", "stall99"] array.each {|x| car.create(stall_number: x)}
Comments
Post a Comment