Rails does not display form error message on a custom method

Hi all,

I’ve created a custom method called checkout in my app. I create an
order (which is done my adding products to my “cart”), assign it to my
client, and then I head to my checkout screen where I confirm the items
and enter their customer order number and complete the order (submit).

Everything works great except that it doesn’t display error messages.
I’m able to display a flash error notice (seen in complete_order method)
when things go wrong but it doesn’t specify the details like a normal
form would. The error messages should appear if the customer order
number is not unique for that client.

Below is the custom method (checkout) related code.

Order Model:

validates_uniqueness_of :customer_order_number, :scope => :client_id


  def checkout
    @order = current_order

  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 }
        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 }


And the form in the checkout view:

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

  <%= f.error_messages %>

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

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

<% end %>

Any idea how I can display the specific error messages?

Thank you in advance! -Tony

Tony T. wrote:

Any idea how I can display the specific error messages?

Thank you in advance! -Tony

Solved thanks to Salil over in
stackoverflow(Rails does not display error messages on a form in a custom method - Stack Overflow).

"Change redirect_to to render in else condition otherwise checkout
method get called again & no error will displayed.

format.html { render :action => ‘checkout’ }"

Thanks Salil!