# Gets errors before submitting form

**URL:** <https://www.ruby-forum.com/t/gets-errors-before-submitting-form/225973>\
**Category:** Rails\
**Created:** [October 22, 2007, 8:41am UTC](https://www.ruby-forum.com/t/gets-errors-before-submitting-form/225973 "2007-10-22T08:41:20Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![elle](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/ccd318/32.png) [@elle](https://www.ruby-forum.com/u/elle)\
**Post date:** [October 22, 2007, 8:41am UTC](https://www.ruby-forum.com/t/gets-errors-before-submitting-form/225973/1 "2007-10-22T08:41:20Z")

</div>

Hi,

When I click on “Proceed to Checkout”, instead of showing a clean  
form, it seems as if the form has already been submitted empty and the  
new page comes up with all the errors – which I don’t think many  
customers would appreciate.  
I am not sure where in the code is the problem.

## My order.rb class is:

def process  
result = true  
#  
# TODO Charge the customer by calling the payment gateway  
# TODO email both admin/customer  
self.status = ‘processed’  
save!  
result  
end

protected

def set\_status  
self.status = “open” if self.status.blank?  
end

## And the cart\_controller.rb has:

def checkout  
@page\_title = “Checkout”  
@order = Order.new(params[:order])  
@order.customer\_ip = request.remote\_ip  
@order.customer\_id = @customer.id  
@order.discount = @customer.discount  
populate\_order

```
if @order.save
  if @order.process
    flash[:notice] = 'Your order has been submitted, and will be

```

processed immediately.’  
session[:order\_id] = @order.id  
# Empty the cart  
@cart.cart\_items.destroy\_all  
redirect\_to :action =\> ‘thank\_you’  
else  
flash[:notice] = “Error while placing order.  
‘#{@order.error\_message}’”  
render :action =\> ‘view\_cart’  
end  
else  
render :action =\> ‘checkout’  
end  
end

def thank\_you  
@page\_title = ‘Thank You!’  
end

def populate\_order  
for cart\_item in @cart.cart\_items  
order\_item = OrderItem.new(  
:product\_id =\> cart\_item.product\_id,  
:price =\> cart\_item.price,  
:amount =\> cart\_item.amount  
)  
@order.order\_items \<\< order\_item  
end  
end

Can anyone have a quick look?

One more question: I am trying to use select\_state plugin in the form.  
My view has:  
State  
\<%= state\_options\_for\_select(selected = nil, country = ‘US’) %\>

But on the page, I only see all the states one after the other instead  
as a drop down menu.  
What’s incorrect in this line?

Thanks,  
Elle

---

<div class="post-metadata">

**Author:** ![elle](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/ccd318/32.png) [@elle](https://www.ruby-forum.com/u/elle)\
**Post date:** [October 24, 2007, 5:39am UTC](https://www.ruby-forum.com/t/gets-errors-before-submitting-form/225973/2 "2007-10-24T05:39:08Z")

</div>

I fixed my state field by using:  
\<%= state\_select(object, method) %\>

But I’m still having troubl with my checkout code. Also If I try and  
submit the order, nothing happens and I’m back at the same form,  
showing errors that no information was entered.

Would anyone have any ideas what is the problem with my code?

TIA,  
Elle

---

<div class="post-metadata">

**Author:** ![elle](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/ccd318/32.png) [@elle](https://www.ruby-forum.com/u/elle)\
**Post date:** [October 24, 2007, 11:56am UTC](https://www.ruby-forum.com/t/gets-errors-before-submitting-form/225973/3 "2007-10-24T11:56:29Z")

</div>

I tried to separate my displaying the checkout form and placing the  
order by spliting the method into:

cart\_controller.rb:  
def checkout  
@page\_title = “Checkout”  
@order = Order.new(params[:order])  
@order.customer\_ip = request.remote\_ip  
@order.customer\_id = @customer.id  
@order.discount = @customer.discount  
populate\_order  
end

def place\_order  
if @order.save  
if @order.process  
flash[:notice] = ‘Your order has been submitted, and will be  
processed immediately.’  
session[:order\_id] = @order.id  
# Empty the cart  
@cart.cart\_items.destroy\_all  
redirect\_to :action =\> ‘thank\_you’  
else  
flash[:notice] = “Error while placing order.  
‘#{@order.error\_message}’”  
render :action =\> ‘view\_cart’  
end  
else  
render :action =\> ‘checkout’  
end  
end

So, now I don’t get any error messages when I click on “Proceed to  
checkout” but, when I click on “Place Order” I get error messages:

NoMethodError in CartController#place\_order

You have a nil object when you didn’t expect it!  
You might have expected an instance of ActiveRecord::Base.  
The error occurred while evaluating nil.save

app/controllers/cart\_controller.rb:99:in `place\_order’

Why is that? don’t I have an order object?

Elle

---

<div class="post-metadata">

**Author:** ![elle](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/ccd318/32.png) [@elle](https://www.ruby-forum.com/u/elle)\
**Post date:** [October 24, 2007, 12:08pm UTC](https://www.ruby-forum.com/t/gets-errors-before-submitting-form/225973/4 "2007-10-24T12:08:46Z")

</div>

> You don’t. Nothing in place\_order creates @order

I thought of that just after I posted this. So I added @order =  
Order.new, which gave me new errors.  
So then I thought of something else and it works.

So, all the code that was in checkout went into  
def place\_order

# original code

end

and  
def checkout

# only shows the checkout form

end

So, all is good.

Cheers,  
Elle

---

<div class="post-metadata">

**Author:** ![elle](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/ccd318/32.png) [@elle](https://www.ruby-forum.com/u/elle)\
**Post date:** [October 24, 2007, 12:01pm UTC](https://www.ruby-forum.com/t/gets-errors-before-submitting-form/225973/5 "2007-10-24T12:01:09Z")

</div>

On 24 Oct 2007, at 10:55, elle wrote:

> ```
> else
> 
> ```
> 
> Why is that? don’t I have an order object?

You don’t. Nothing in place\_order creates @order

Fred

---

<div class="post-metadata">

**Author:** ![elle](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/ccd318/32.png) [@elle](https://www.ruby-forum.com/u/elle)\
**Post date:** [October 24, 2007, 12:11pm UTC](https://www.ruby-forum.com/t/gets-errors-before-submitting-form/225973/6 "2007-10-24T12:11:52Z")

</div>

Hi,  
I think you can’t use a variable of one action in another like this  
Try as  
session[:order] = @order  
in def check out  
and  
in def place\_order  
if session[:order].save

Hope it helps
