Screencast: Mehrere Daten gleichzeitig bearbeiten
Mehrere Einträge in Formular gleichtzeitig zu bearbeiten erfordert gewisse Anpassungen bzw. Vorkehrngen. Eine Möglichkeit wie dies gelöst werden kann zeigt Ryan diese Woche in seinem Screencast.
Download
Download (33.6 MB, 13:53)
Alternativer Download für iPod & Apple TV (19.8 MB, 13:53)
Ressourcen:
Quellcode:
# 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 }
<!-- views/products/index.html.erb -->
<% form_tag edit_individual_products_path do %>
<table>
<tr>
<th></th>
<th>Name</th>
<th>Category</th>
<th>Price</th>
</tr>
<% for 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 you 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 %>
<!-- views/products/edit_individual.html.erb -->
<% form_tag update_individual_products_path, :method => :put do %>
<% for product in @products %>
<% fields_for "products[]", product do |f| %>
<h2><%=h product.name %></h2>
<%= render "fields", :f => f %>
<% end %>
<% end %>
<p><%= submit_tag "Submit" %></p>
<% end %>
<!-- views/products/_fields.html.erb -->
<%= f.error_messages :object_name => "product" %>
<% if params[:field].blank? || params[:field] == "name" %>
<p>
<%= f.label :name %><br />
<%= f.text_field :name %>
</p>
<% end %>
<% if params[:field].blank? || params[:field] == "price" %>
<p>
<%= f.label :price %><br />
<%= f.text_field :price %>
</p>
<% end %>
<% if params[:field].blank? || params[:field] == "category_id" %>
<p>
<%= f.label :category_id %><br />
<%= f.collection_select :category_id, Category.all, :id, :name %>
</p>
<% end %>
<% if params[:field].blank? || params[:field] == "discontinued" %>
<p>
<%= f.check_box :discontinued %>
<%= f.label :discontinued %>
</p>
<% end %>
Eingestellt am 25.01.2010 um 14:11
Kategorie: Tutorials
Suchen auf rubyonrails.de
Tags
2.0 2.1 Active-Record Ajax Buch Capistrano Enterprise Event Gewinnspiel Grundlagen IDE irb jQuery Kochbuch Konferenz Mac Mongrel OpenRoRBook OReilly OSX Passenger Phusion Plugin PragProgs Rails Rails 3 RailsKonf RailsWay RailsWayCon Release Release Candidate Routing Ruby Ruby 1.9 RubyGems rubyonrails.de Ryan Bates Scaffolding Screencast Script-Tip Security Tutorial Video Views WindowsAktuelle Artikel
- Freikarte für die RailsWayCon 2010
- Screencast: Schutz vor XSS in Rails 3
- Phusion Passenger 2.2.11 erschienen
- Script-Tip: Summe eines Arrays ermitteln
- Vortrag zu Rails 3 von Yehuda Katz
- Hobo 1.0 veröffentlicht
- Screencast: Routes in Rails 3
- 5 freie Kapitel aus Ruby Best Practices
- Phusion Passenger 2.2.10 veröffentlicht
- Screencast: ActiveRecord Anfragen in Rails 3
Artikel Archiv
- March 2010
- February 2010
- January 2010
- December 2009
- November 2009
- October 2009
- September 2009
- August 2009
- July 2009
- May 2009
- April 2009
- March 2009
- February 2009
- December 2008
- November 2008
- August 2008
- July 2008
- June 2008
- May 2008
- March 2008
- February 2008
- January 2008
- December 2007
- November 2007
- October 2007
- September 2007
Aktuelle Rails Version + Abhängigkeiten:
rails-2.3.5actionmailer (= 2.3.5, runtime)
actionpack (= 2.3.5, runtime)
activerecord (= 2.3.5, runtime)
activeresource (= 2.3.5, runtime)
activesupport (= 2.3.5, runtime)
rake (>= 0.8.3, runtime)
Rails auf Rubyforge
Rails auf Github
