Is this action ok? Can it be improved in any way? Should I extract the setting up of the order to a private method and just call that? Or is it fine and in line with the ‘Rails way’?
def create
@order = Order.new(params[:order])
@product = Product.find(session[:product])
@order.amount = session[:total_amount]
@order.ip_address = request.remote_ip
@order.product_id = @product.id
@order.product_price = @product.price
@order.voucher = @voucher_value
@order.friend_id = session[:friend_id]
if @order.save
if @order.purchase
render :action => "success"
reset_friend_session_codes
else
render :action => "failure"
end
else
render :action => 'new'
end
end
TIA.
I would factor out as much as I could into a private method in the order model and call it with one of the model hooks, such as
before_validation. That’s too much logic for an action, and tramples all over MVC.