· 2 min read
Screencast: Mehrere Daten gleichzeitig bearbeiten
Download
Download (33.6 MB, 13:53) Alternativer Download für iPod & Apple TV (19.8 MB, 13:53)
Ressourcen:
Quellcode:
[ruby] # products_controller.rb def edit_individual @products = Product.find(params[:product_ids]) end
def update_individual @products = Product.update(params[:products].keys, params[:products].values).reject { |p| p.errors.empty? } if @products.empty? flash[:notice] = “Products updated” redirect_to products_url else render :action => “edit_individual” end end
# routes.rb map.resources :products, :collection => { :edit_individual => :post, :update_individual => :put } [/ruby]
[html] <% form_tag edit_individual_products_path do %>
<% for product in @products %> <% end %>Name | Category | Price | |||
---|---|---|---|---|---|
<%= check_box_tag “product_ids[]”, product.id %> | <%=h product.name %> | <%=h product.category.name %> | <%= number_to_currency product.price %> | <%= link_to “Edit”, edit_product_path(product) %> | <%= link_to “Destroy”, product, :confirm => ‘Are you sure?’, :method => :delete %> |
<%= select_tag :field, options_for_select([[“All Fields”, ""], [“Name”, “name”], [“Price”, “price”], [“Category”, “category_id”], [“Discontinued”, “discontinued”]]) %> <%= submit_tag “Edit Checked” %>
<% end %><% form\_tag update\_individual\_products\_path, :method => :put do %> <% for product in @products %> <% fields\_for "products\[\]", product do |f| %><%=h product.name %>
<%= render "fields", :f => f %> <% end %> <% end %><%= submit\_tag "Submit" %>
<% end %> <%= f.error\_messages :object\_name => "product" %> <% if params\[:field\].blank? || params\[:field\] == "name" %><%= f.label :name %>
<%= f.text\_field :name %>
<%= f.label :price %>
<%= f.text\_field :price %>
<%= f.label :category\_id %>
<%= f.collection\_select :category\_id, Category.all, :id, :name %>
<%= f.check\_box :discontinued %> <%= f.label :discontinued %>
<% end %> \[/html\]