Screencast: Mehrstufige Formulare
Je nach nach Umfang und Komplexität ist es notwendig ein Formular in mehrere Schritte aufzuteilen. Diese Woche zeigt Ryan in seinem Screencast, wie dies mit Rails gelöst werden kann.
Download:
Download(21.8 MB, 15:14)
Alternativer Downloadfür iPod & Apple TV(20.9 MB, 15:14)
Resourcen:
Quellcode:
script/generate nifty_scaffold order shipping_name:string billing_name:string index show new
# models/order.rb
attr_writer :current_step
validates_presence_of :shipping_name, :if => lambda { |o| o.current_step == "shipping" }
validates_presence_of :billing_name, :if => lambda { |o| o.current_step == "billing" }
def current_step
@current_step || steps.first
end
def steps
%w[shipping billing confirmation]
end
def next_step
self.current_step = steps[steps.index(current_step)+1]
end
def previous_step
self.current_step = steps[steps.index(current_step)-1]
end
def first_step?
current_step == steps.first
end
def last_step?
current_step == steps.last
end
def all_valid?
steps.all? do |step|
self.current_step = step
valid?
end
end
# orders_controller.rb
def new
session[:order_params] ||= {}
@order = Order.new(session[:order_params])
@order.current_step = session[:order_step]
end
def create
session[:order_params].deep_merge!(params[:order]) if params[:order]
@order = Order.new(session[:order_params])
@order.current_step = session[:order_step]
if @order.valid?
if params[:back_button]
@order.previous_step
elsif @order.last_step?
@order.save if @order.all_valid?
else
@order.next_step
end
session[:order_step] = @order.current_step
end
if @order.new_record?
render "new"
else
session[:order_step] = session[:order_params] = nil
flash[:notice] = "Order saved!"
redirect_to @order
end
end
<!-- orders/new.html.erb -->
<% form_for @order do |f| %>
<%= f.error_messages %>
<%= render "#{@order.current_step}_step", :f => f %>
<p><%= f.submit "Continue" %></p>
<p><%= f.submit "Back", :name => "back_button" unless @order.first_step? %></p>
<% end %>
Eingestellt am 07.06.2010 um 12:53
Kategorie: Tutorials
Suchen auf rubyonrails.de
Tags
2.0 ActiveRecord Ajax Authentication Buch Capistrano Controller Enterprise Event Formulare Gewinnspiel Grundlagen IDE irb jQuery Kochbuch Konferenz MySQL OReilly Passenger Phusion Plugin PragProgs Rails Rails 3 Rails 3.1 RailsWayCon Release Release Candidate Routing Ruby RubyGems rubyonrails.de Ryan Bates Scaffolding Screencast Script-Tip Security Test Tools Tutorial Upgrade Video Views WindowsAktuelle Artikel
- Screencast: JSON Dokumente mit Jbuilder erstellen
- Rails 3.2.1 veröffentlicht
- Screencast: Upgrade auf Rails 3.2
- Ruby on Rails 3.2
- Screencast: Private Pub für Realtime-Events
- Screencast: Pretty URLs mit FriendlyId
- Screencast: HTML Emails mit Rails verschicken
- Screencast: Einstieg in die Rails-Entwicklung
- Frohe Weihnachten!
- Screencast: Arbeit an der Konsole mit ZSH
Artikel Archiv
- January 2012
- December 2011
- November 2011
- October 2011
- September 2011
- August 2011
- July 2011
- June 2011
- May 2011
- April 2011
- March 2011
- February 2011
- January 2011
- December 2010
- November 2010
- October 2010
- September 2010
- August 2010
- July 2010
- June 2010
- May 2010
- April 2010
- 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:
Gem rails-3.0.0actionmailer (= 3.0.0, runtime)
actionpack (= 3.0.0, runtime)
activerecord (= 3.0.0, runtime)
activeresource (= 3.0.0, runtime)
activesupport (= 3.0.0, runtime)
bundler (~> 1.0.0, runtime)
railties (= 3.0.0, runtime)
Rails auf Rubyforge
Rails auf Github
