我在我的应用中创建了一个名为checkout的自定义方法。我创建了一个订单(这是我将产品添加到我的“购物车”),将其分配给我的客户,然后我前往我的结账屏幕,我确认这些项目并输入他们的客户订单号并完成订单(提交)
除了不显示错误消息外,一切都很有效。当出现问题时我能够显示闪存错误通知(在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 %>
知道如何显示特定的错误消息吗?
答案 0 :(得分:2)
在其他情况下将 redirect_to 更改为呈现,否则再次调用checkout方法&amp;不会显示错误。
else
format.html { render :action => 'checkout' }