=f.fields_for :customer
ここで説明されているように、複数ステップ フォームの 2 番目の部分に挿入しています。フォームの最初のステップ (_voucher_partial.html.haml) にある場合は問題なく動作しますが、2 番目のステップに移動するとまったく表示されません。誰でも理由と修正方法を教えてもらえますか? なぜ Rails が異なる扱いをするのか、私には理解できません。
ありがとう!
注文
class Order < ActiveRecord::Base
attr_accessible :customer_attributes, :customer_id, :format
belongs_to :customer
accepts_nested_attributes_for :customer
attr_writer :current_step
def steps
%w[voucher recipient delivery confirmation]
end
def current_step
@current_step || steps.first
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
注文コントローラー
# GET /orders/new
# GET /orders/new.json
def new
session[:order_params] ||= {}
@order = Order.new(session[:order_params])
@order.build_customer(session[:order_params])
@order.current_step = session[:order_step]
end
# POST /orders
# POST /orders.json
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 params[:back_button]
@order.previous_step
elsif @order.last_step?
@order.save
else
@order.next_step
end
session[:order_step] = @order.current_step
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.haml
%h1 New order
= form_for(@order) do |f|
- if @order.errors.any?
#error_explanation
%h2
= pluralize(@order.errors.count, "error")
prohibited this order from being saved:
%ul
- @order.errors.full_messages.each do |msg|
%li= msg
= render "#{@order.current_step}_step", :f => f
= f.submit "Continue"
= f.submit "Back", :name => "back_button" unless @order.first_step?
= link_to 'Back', orders_path
_recipient_step.html.haml
%h3
Customer details
=f.fields_for :customer do |builder|
=builder.label :name
=builder.text_field :name
=builder.label :email
=builder.text_field :email
%h3
Recipient details
.field
= f.label :recipient_name
= f.text_field :recipient_name