2010-04-14 46 views
0

我在我的應用程序中創建了一個名爲checkout的自定義方法。我創建了一個訂單(這是我的產品添加到我的「購物車」後完成的),將其分配給我的客戶,然後前往我的結賬屏幕,我確認這些項目並輸入他們的客戶訂單號並完成訂單(提交) 。Rails不在自定義方法中的窗體上顯示錯誤消息

除非不顯示錯誤信息,否則一切都很好。當出現錯誤時,我能夠顯示閃存錯誤通知(在complete_order方法中看到),但它沒有像普通格式那樣指定細節。如果客戶訂單號對於該客戶不唯一,則應出現錯誤消息。

下面是自定義方法(checkout)的相關代碼。

訂貨型號:

validates_uniqueness_of :customer_order_number, :scope => :client_id 

Orders_controller:

def checkout 
    @order = current_order 
    end 

    def complete_order 
    @order = current_order 

    respond_to do |format| 
     if @order.update_attributes(params[:order]) 
     @order.complete #sets submitted datetime and state to 'complete' 
     flash[:notice] = 'Thank you! Your order is being processed.' 
     format.html { redirect_to(products_path) } 
     format.xml { head :ok } 
     else 
     flash[:error] = 'Please review your items' #added to confirm an error is present 
     format.html { redirect_to(checkout_path) } 
     format.xml { render :xml => @order.errors, :status => :unprocessable_entity } 
     end 
    end 

    end 

並在結賬形式來看:

<% form_for @order, :url => { :controller => "orders", :action => "complete_order" } do |f| %> 

    <%= f.error_messages %> 


    <%= f.text_field :customer_order_number, :label => "Purchase Order Number" %> 

    <p> 
    <%= f.submit 'Complete Order', :confirm => 'Are you sure?' %> <small> or <%= link_to 'cancel', current_cart_path %></small> 
    </p> 

<% end %> 

任何想法如何顯示特定的錯誤消息?

回答

2

變化redirect_to的呈現在其他條件,否則結賬方法再次得到&叫會顯示錯誤。

else 
    format.html { render :action => 'checkout' } 
+0

太簡單了!謝謝! – slythic 2010-04-14 14:50:04

相關問題