# Rails does not display form error message on a custom method

**URL:** <https://www.ruby-forum.com/t/rails-does-not-display-form-error-message-on-a-custom-method/186937>\
**Category:** Rails\
**Created:** [April 14, 2010, 4:34pm UTC](https://www.ruby-forum.com/t/rails-does-not-display-form-error-message-on-a-custom-method/186937 "2010-04-14T16:34:48Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![bagadoughnuts](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/71c47a/32.png) [@bagadoughnuts](https://www.ruby-forum.com/u/bagadoughnuts)\
**Post date:** [April 14, 2010, 4:34pm UTC](https://www.ruby-forum.com/t/rails-does-not-display-form-error-message-on-a-custom-method/186937/1 "2010-04-14T16:34:48Z")

</div>

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:

```auto
validates_uniqueness_of :customer_order_number, :scope => :client_id
```

Orders\_controller:

```auto
  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
```

And the form in the checkout view:

```auto
<% 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 %>
```

Any idea how I can display the specific error messages?

Thank you in advance! -Tony

---

<div class="post-metadata">

**Author:** ![bagadoughnuts](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/71c47a/32.png) [@bagadoughnuts](https://www.ruby-forum.com/u/bagadoughnuts)\
**Post date:** [April 14, 2010, 4:52pm UTC](https://www.ruby-forum.com/t/rails-does-not-display-form-error-message-on-a-custom-method/186937/2 "2010-04-14T16:52:32Z")

</div>

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](http://stackoverflow.com/questions/2638215/rails-does-not-display-error-messages-on-a-form-in-a-custom-method)).

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

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

Thanks Salil!
