Rails loop through data in a form field -
i loop through data in database inside form. data put in labels , textboxes. how in rails? use .each block loop through inside form? reason have in database because client able add form field data himself.
for example here do:
<%= form_for :order |f| %> @fields.each |field| <%= f.label field.name %> <%= f.text_field field.name %> <% end %> <%= f.submit %> <% end %>
what best way accomplish this?
please don't answer railscast :)
thanks in advance
yes, work, though missed end script tag on line two:
<%= form_for :order |f| %> <% @fields.each |field| %> <%= f.label field.name %> <%= f.text_field field.name %> <% end %> <%= f.submit %> <% end %>
if need more complex label/text field pair - can use partial-template , use collection keyword:
<!-- in 'order.html.erb' --> <%= form_for :order |f| %> <!-- note: each 'field' auto-populated collection/partial-name, need pass form in local --> <%= render :partial => 'field', :collection => @fields, :locals => {:f => f} %> <%= f.submit %> <% end %>
and
<!-- in '_field.html.erb' --> <%= f.label field.name %> <%= f.text_field field.name %> <!-- , whatever else want do... -->
more on partial rendering here: http://api.rubyonrails.org/classes/actionview/partials.html
Comments
Post a Comment