一个脚手架生成这样的新动作:
def new
@product = Product.new
respond_to do |format|
format.html # new.html.erb
format.xml { render :xml => @product }
end
end
def create
@product = Product.new(params[:product])
respond_to do |format|
if @product.save
format.html { redirect_to(@product, :notice => 'Product was successfully created.') }
format.xml { render :xml => @product, :status => :created, :location => @product }
else
format.html { render :action => "new" }
format.xml { render :xml => @product.errors, :status => :unprocessable_entity }
end
end
end
并且视图呈现一个局部的,命名为form
. 既然new
表单呈现了创建新产品的操作集,那么目的是@product
什么?我看到该create
操作也实例化了一个新对象。它是否仅用于将表单与对象联系起来,以便一切都正确地从动作到动作?