ruby on rails - Railscast 198, but using formtastic -
how what's covered in ryanb's railscast on editing multiple records individually, using formtastic? formtastic doesn't use form_tag, ryanb's method relies on.
the semantic_form_for
wrapper around form_for
can use same parameters. here formtastic version of ryan bates' screencast
views/products/edit_individual.html.erb
<% semantic_form_for :update_individual_products, :url => update_individual_products_path, :method => :put |f| %> <% product in @products %> <% f.fields_for "products[]", product |ff| %> <h2><%=h product.name %></h2> <%= render "fields", :f => ff %> <% end %> <% end %> <p><%= submit_tag "submit" %></p> <% end %>
views/products/index.html.erb
<% semantic_form_for :edit_individual_products, :url => edit_individual_products_path %> <table> <tr> <th></th> <th>name</th> <th>category</th> <th>price</th> </tr> <% product in @products %> <tr> <td><%= check_box_tag "product_ids[]", product.id %></td> <td><%=h product.name %></td> <td><%=h product.category.name %></td> <td><%= number_to_currency product.price %></td> <td><%= link_to "edit", edit_product_path(product) %></td> <td><%= link_to "destroy", product, :confirm => 'are sure?', :method => :delete %></td> </tr> <% end %> </table> <p> <%= select_tag :field, options_for_select([["all fields", ""], ["name", "name"], ["price", "price"], ["category", "category_id"], ["discontinued", "discontinued"]]) %> <%= submit_tag "edit checked" %> </p> <% end %>
please note can use form_for
helpers in formtastic
.
update
if use nested attributes should work out of box, using fields_for on form partial. lets stick railscast example , that:
product.rb
has_many :commments accepts_nested_attributes_for :comments
you can edit comments on _fields.html.erb of products like:
<%= f.fields_for :comments |cf| %> <%=render 'comments/fields', :f=>cf%> <%end%>
and make sure have fields partial in comments views.
Comments
Post a Comment