2012-03-01 71 views
0

這個動作好嗎?它可以以任何方式改進嗎?我是否應該將訂單的設置提取到私有方法,然後調用它?或者它是否正常並符合'Rails方式'?我可以改進此控制器操作嗎?

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。

+0

您可能還想在http://codereview.stackexchange.com/上發佈此信息,並查看您得到的答覆。 – ScottJShea 2012-03-01 23:44:34

+0

謝謝!張貼在那裏,我現在應該刪除這個嗎? – eBrooker 2012-03-02 00:09:01

+0

如果人們開始投票,但是考慮到有人發佈了答案,這似乎沒問題。 – ScottJShea 2012-03-02 00:46:58

回答

0

我將盡可能多地分解到訂單模型中的私有方法,並使用模型掛鉤之一調用它,例如before_validation。這對於一個動作來說太過於邏輯了,並且踐踏了所有的MVC。

+0

嗨,感謝您的回覆。我將如何獲得會話信息和其他變量到鉤子方法? (不需要使用虛擬屬性,因爲這意味着它最終會與上面幾乎一樣?) – eBrooker 2012-03-02 02:37:52

+0

設置虛擬屬性是沒有意義的,但是可以在沒有會話的情況下完成任何這些行數據應該移到模型中。 – Preacher 2012-03-02 03:14:13

相關問題