# Routing Error

**URL:** <https://www.ruby-forum.com/t/routing-error/170471>\
**Category:** Rails\
**Created:** [June 17, 2009, 11:42pm UTC](https://www.ruby-forum.com/t/routing-error/170471 "2009-06-17T23:42:22Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![knappe](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/5fc32e/32.png) [@knappe](https://www.ruby-forum.com/u/knappe)\
**Post date:** [June 17, 2009, 11:42pm UTC](https://www.ruby-forum.com/t/routing-error/170471/1 "2009-06-17T23:42:22Z")

</div>

I have a problem with my routing. When a user performs an illegal  
checkout action (end time \< start time) it should route back to to the  
‘checkout’ action. However, it is instead going to the ‘edit’ action.

**‘rake routes’**

users GET /users(.:format) {:controller=\>“users”,  
:action=\>“index”}  
POST /users(.:format)  
{:controller=\>“users”, :action=\>“create”}  
new\_user GET /users/new(.:format)  
{:controller=\>“users”, :action=\>“new”}  
edit\_user GET /users/:id/edit(.:format)  
{:controller=\>“users”, :action=\>“edit”}  
user GET /users/:id(.:format)  
{:controller=\>“users”, :action=\>“show”}  
PUT /users/:id(.:format)  
{:controller=\>“users”, :action=\>“update”}  
DELETE /users/:id(.:format)  
{:controller=\>“users”, :action=\>“destroy”}  
new\_session GET /session/new(.:format)  
{:controller=\>“sessions”, :action=\>“new”}  
edit\_session GET /session/edit(.:format)  
{:controller=\>“sessions”, :action=\>“edit”}  
session GET /session(.:format)  
{:controller=\>“sessions”, :action=\>“show”}  
PUT /session(.:format)  
{:controller=\>“sessions”, :action=\>“update”}  
DELETE /session(.:format)  
{:controller=\>“sessions”, :action=\>“destroy”}  
POST /session(.:format)  
{:controller=\>“sessions”, :action=\>“create”}  
labs GET /labs(.:format)  
{:controller=\>“labs”, :action=\>“index”}  
POST /labs(.:format)  
{:controller=\>“labs”, :action=\>“create”}  
new\_lab GET /labs/new(.:format)  
{:controller=\>“labs”, :action=\>“new”}  
edit\_lab GET /labs/:id/edit(.:format)  
{:controller=\>“labs”, :action=\>“edit”}  
lab GET /labs/:id(.:format)  
{:controller=\>“labs”, :action=\>“show”}  
PUT /labs/:id(.:format)  
{:controller=\>“labs”, :action=\>“update”}  
DELETE /labs/:id(.:format)  
{:controller=\>“labs”, :action=\>“destroy”}  
lab\_categories GET /lab\_categories(.:format)  
{:controller=\>“lab\_categories”, :action=\>“index”}  
POST /lab\_categories(.:format)  
{:controller=\>“lab\_categories”, :action=\>“create”}  
new\_lab\_category GET /lab\_categories/new(.:format)  
{:controller=\>“lab\_categories”, :action=\>“new”}  
edit\_lab\_category GET /lab\_categories/:id/edit(.:format)  
{:controller=\>“lab\_categories”, :action=\>“edit”}  
lab\_category GET /lab\_categories/:id(.:format)  
{:controller=\>“lab\_categories”, :action=\>“show”}  
PUT /lab\_categories/:id(.:format)  
{:controller=\>“lab\_categories”, :action=\>“update”}  
DELETE /lab\_categories/:id(.:format)  
{:controller=\>“lab\_categories”, :action=\>“destroy”}  
checkout\_index GET /checkout(.:format)  
{:controller=\>“checkout”, :action=\>“index”}  
POST /checkout(.:format)  
{:controller=\>“checkout”, :action=\>“create”}  
new\_checkout GET /checkout/new(.:format)  
{:controller=\>“checkout”, :action=\>“new”}  
edit\_checkout GET /checkout/:id/edit(.:format)  
{:controller=\>“checkout”, :action=\>“edit”}  
GET /checkout/:id(.:format)  
{:controller=\>“checkout”, :action=\>“show”}  
PUT /checkout/:id(.:format)  
{:controller=\>“checkout”, :action=\>“update”}  
DELETE /checkout/:id(.:format)  
{:controller=\>“checkout”, :action=\>“destroy”}  
login /  
{:controller=\>“sessions”, :action=\>“new”}  
signup /signup  
{:controller=\>“users”, :action=\>“new”}  
myaccount /myaccount  
{:controller=\>“myaccount”, :action=\>“myaccount”}  
logout /logout  
{:controller=\>“sessions”, :action=\>“destroy”}  
/labs/checkout/:id  
{:controller=\>“checkout”, :action=\>“checkout”}  
/labs/checkout/:id/update  
{:controller=\>“checkout”, :action=\>“update”}  
checkout /labs/checkout/:id/edit  
{:controller=\>“checkout”, :action=\>“update”}  
root /  
{:controller=\>“home”, :action=\>“index”}  
/:controller/:action/:id  
/:controller/:action/:id(.:format)

routes.rb:  
map.checkout ‘/labs/checkout/:id’, :controller =\> ‘checkout’, :action  
=\> ‘checkout’  
map.checkout ‘/labs/checkout/:id/update’, :controller =\> ‘checkout’,  
:action =\> ‘update’  
map.checkout ‘/labs/checkout/:id/edit’, :controller =\> ‘checkout’,  
:action =\> ‘update’  
map.root :controller =\> “home”

# default routes

map.connect ‘:controller/:action/:id’  
map.connect ‘:controller/:action/:id.:format’

**CheckoutController update action:**

def update  
@lab = Lab.find(params[:id])

```
            respond_to do |format|
                    if @lab.update_attributes(params[:lab])
                            flash[:notice] = 'Lab was successfully

```

updated.’  
format.html { redirect\_to(@lab) }  
format.xml { head :ok }  
else  
format.html { render :action =\>  
“checkout” }  
format.xml { render :xml =\>  
@lab.errors, :status =\> :unprocessable\_entity }  
end  
end  
end

**LabController update**

def update  
@lab = Lab.find(params[:id])

```
respond_to do |format|
  if @lab.update_attributes(params[:lab])
    flash[:notice] = 'Lab was successfully updated.'
    format.html { redirect_to(@lab) }
    format.xml { head :ok }
  else
    format.html { render :action => "edit" }
    #format.html { redirect_to :back }
    format.xml { render :xml => @lab.errors, :status =>

```

:unprocessable\_entity }  
end  
end  
end

Let me know if there is something I forgot to include.

Thanks

---

<div class="post-metadata">

**Author:** ![knappe](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/5fc32e/32.png) [@knappe](https://www.ruby-forum.com/u/knappe)\
**Post date:** [June 18, 2009, 12:09am UTC](https://www.ruby-forum.com/t/routing-error/170471/2 "2009-06-18T00:09:42Z")

</div>

I think the issue is that you have a general route to the checkout  
controller and then you have a map.checkout route that is after the  
map.resources :checkout route. so when you use checkout\_path you will  
get the edit task and not the checkout “route” that you have setup…

Try changing map.checkout to map.checkout-something-else and see if  
that fixes the issue…

On Jun 17, 2:42 pm, Tyler K. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![knappe](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/5fc32e/32.png) [@knappe](https://www.ruby-forum.com/u/knappe)\
**Post date:** [June 18, 2009, 4:14pm UTC](https://www.ruby-forum.com/t/routing-error/170471/3 "2009-06-18T16:14:37Z")

</div>

heimdull wrote:

> I think the issue is that you have a general route to the checkout  
> controller and then you have a map.checkout route that is after the  
> map.resources :checkout route. so when you use checkout\_path you will  
> get the edit task and not the checkout “route” that you have setup…
> 
> Try changing map.checkout to map.checkout-something-else and see if  
> that fixes the issue…
> 
> On Jun 17, 2:42ï¿½pm, Tyler K. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

I think the problem lies here:

def update  
@lab = Lab.find(params[:id])

```
respond_to do |format|
  if @lab.update_attributes(params[:lab])
    flash[:notice] = 'Lab was successfully updated.'
    format.html { redirect_to(@lab) }
    format.xml { head :ok }
  else
    format.html { render :action => "edit" }

```

If an unsuccessful update occurs the edit action is called, I changed  
this to another action and that action was called. However, what I  
can’t figure out is why this action is called from the Labs Controller,  
not from the Checkout Controller, where the checkout is called. I have  
an update action in the Checkout Controller…

---

<div class="post-metadata">

**Author:** ![knappe](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/5fc32e/32.png) [@knappe](https://www.ruby-forum.com/u/knappe)\
**Post date:** [June 18, 2009, 5:52pm UTC](https://www.ruby-forum.com/t/routing-error/170471/4 "2009-06-18T17:52:33Z")

</div>

Tyler K. wrote:

> heimdull wrote:
> 
> > I think the issue is that you have a general route to the checkout  
> > controller and then you have a map.checkout route that is after the  
> > map.resources :checkout route. so when you use checkout\_path you will  
> > get the edit task and not the checkout “route” that you have setup…
> > 
> > Try changing map.checkout to map.checkout-something-else and see if  
> > that fixes the issue…
> > 
> > On Jun 17, 2:42ï¿½pm, Tyler K. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)
> 
> You were on the right track. What I had not realized was that  
> map.resources :controller generates the routes that I was seeing. I  
> moved my map.checkout action to the top of the list and it now properly  
> calls the checkout controller’s update function!

I was mistaken, I had changed how the Labs update action behaved. I’m  
still having the same problem and I’ve changed all the routes around.

---

<div class="post-metadata">

**Author:** ![knappe](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/5fc32e/32.png) [@knappe](https://www.ruby-forum.com/u/knappe)\
**Post date:** [June 18, 2009, 4:43pm UTC](https://www.ruby-forum.com/t/routing-error/170471/5 "2009-06-18T16:43:49Z")

</div>

heimdull wrote:

> I think the issue is that you have a general route to the checkout  
> controller and then you have a map.checkout route that is after the  
> map.resources :checkout route. so when you use checkout\_path you will  
> get the edit task and not the checkout “route” that you have setup…
> 
> Try changing map.checkout to map.checkout-something-else and see if  
> that fixes the issue…
> 
> On Jun 17, 2:42ï¿½pm, Tyler K. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

You were on the right track. What I had not realized was that  
map.resources :controller generates the routes that I was seeing. I  
moved my map.checkout action to the top of the list and it now properly  
calls the checkout controller’s update function!
