maeshimaの日記

メモ書きです

今週のrailscasts - Multistep Forms

Railscasts - Multistep Formsを見て。

formの内容が多くて複数ページに分割する場合のtips。

基本的な流れ

  1. それぞれの input 部分を partial templateにする
  2. ページ毎に名前を付けて配列で格納
  3. render :partial => "" の部分を session で切り替える
  4. session でページや入力の状態を保持する

特に難しいことはやっていない印章。

気になったところ

本筋とは違うけど気になったところをメモ。

下記のようなステップのインクリメント&デクリメント方法

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

あと戻るボタンの処理。下記のようにnameにback_buttonと入れておいて

<%= f.submit "Back", :name => "back_button" unless @order.first_step? %>

内部で params[:back_button] の有無を見て振り分けてる

if params[:back_button]
  @order.previous_step
  # ...