我在这里描述的多步形式的第二部分中插入=f.fields_for :customer
。如果它在表单的第一步(_voucher_partial.html.haml),它可以正常工作,但如果我将它移动到第二步,它将不会显示。任何人都可以告诉我为什么,以及如何解决?我不明白为什么他们会被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
%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
%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
答案 0 :(得分:0)
你能尝试用这种方式渲染部分:
= render partial: "#{@order.current_step}_step", object: @order, locals: { :f => f }
顺便说一句,在您的创建操作中,您始终会创建一个新订单,但不会重建客户!