# RESTful design/url question

**URL:** <https://www.ruby-forum.com/t/restful-design-url-question/85554>\
**Category:** Rails\
**Created:** [February 14, 2007, 3:26pm UTC](https://www.ruby-forum.com/t/restful-design-url-question/85554 "2007-02-14T15:26:36Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Meech](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/0ea827/32.png) [@Meech](https://www.ruby-forum.com/u/Meech)\
**Post date:** [February 14, 2007, 3:26pm UTC](https://www.ruby-forum.com/t/restful-design-url-question/85554/1 "2007-02-14T15:26:36Z")

</div>

New to rails and restful design, forgive me if this has been asked…

Lets say you have customers and orders models. orders belongs\_to  
customers.

When viewing a customer, (/customers/1/) I want to present the option  
to view the customers orders (and eventually invoices, POs, etc).  
What route would be within restful guidelines?

/customers/1:orders  
/customers/1?view=orders  
/customers/1/orders/  
/orders:filter?customer=1

I think I would prefer each “page” of a customer detail have it’s own  
controller method – this will allow me to keep the security decisions  
completely within the controller easily.

Thanks

---

<div class="post-metadata">

**Author:** ![Meech](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/0ea827/32.png) [@Meech](https://www.ruby-forum.com/u/Meech)\
**Post date:** [February 14, 2007, 4:18pm UTC](https://www.ruby-forum.com/t/restful-design-url-question/85554/2 "2007-02-14T16:18:43Z")

</div>

Thanks a bunch.

Follow-up question if you don’t mind…

You are on the page to view a customers orders:  
/customers/1/orders/

User clicks edit, Would you replicate the edit, ala:

/customers/1/orders/1;edit

Or kick 'em to the orders resource?

/orders/1;edit

I’d like to be able to click “New Order” from within the customer view,  
something like

/customers/1/orders/new

But the replication doesn’t feel very DRY.

---

<div class="post-metadata">

**Author:** ![Meech](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/0ea827/32.png) [@Meech](https://www.ruby-forum.com/u/Meech)\
**Post date:** [February 14, 2007, 3:44pm UTC](https://www.ruby-forum.com/t/restful-design-url-question/85554/3 "2007-02-14T15:44:38Z")

</div>

On Feb 14, 8:25 am, “Meech” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> New to rails and restful design, forgive me if this has been asked…

Not at all.

> /orders:filter?customer=1  
> I believe it would be /customers/1/orders/ . You achieve this by  
> specifying that orders are nested within customers in your routes.rb:

map. resources :customers do |customer|  
customer.resources :orders  
end

and then, in your OrdersController class, use a before\_filter to  
identify the customer, something like:

before\_filter :get\_customer

def get\_customer  
@customer = Customer.find(params[:customer\_id])  
end

The customer\_id key is automatically created for you when you nest the  
routes.

> I think I would prefer each “page” of a customer detail have it’s own  
> controller method – this will allow me to keep the security decisions  
> completely within the controller easily.

That’s how Rails works anyway - each “page” is an “action”. An action  
is a coupling of a controller method and a template (.rhtml page). So  
you’re on the right track.

Hope this helps  
Jeff

> **[Hey Daddio!](http://softiesonrails.com)**
>
> Home of Hey Daddio Software

---

<div class="post-metadata">

**Author:** ![Meech](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/0ea827/32.png) [@Meech](https://www.ruby-forum.com/u/Meech)\
**Post date:** [February 14, 2007, 4:25pm UTC](https://www.ruby-forum.com/t/restful-design-url-question/85554/4 "2007-02-14T16:25:45Z")

</div>

you can use the builtin helper:

edit\_order\_path(@customer,@order)

that will create a link like “/customers/1/order/1;edit”

new\_order\_path(@customer) will create a link to “/customer/1/order/new”

ed

---

<div class="post-metadata">

**Author:** ![Meech](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/0ea827/32.png) [@Meech](https://www.ruby-forum.com/u/Meech)\
**Post date:** [February 14, 2007, 5:29pm UTC](https://www.ruby-forum.com/t/restful-design-url-question/85554/5 "2007-02-14T17:29:56Z")

</div>

I understand that,

But in my instance orders is not only a child of customers, it’s a  
bonafide  
resource of it’s own, accessible via:

/orders/  
/orders/1/  
/orders/1;edit

So, if we are viewing a customer/orders page, /customers/1/orders/ and  
they click on “Edit Order”. Do we goto:

/customers/1/orders/1;edit #edit order #1  
Or  
/orders/1;edit #edit order #1

Do we implement both methods for the same function?

Syntactically, I think I like the both approach. If a user clicks on  
“Edit  
Order” from within a customer orders details view, its pretty apparent  
where  
they should return after completing the edit.

* * *

From: removed\_email\_address@domain.invalid  
[[mailto:removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)] On Behalf Of Ed Hickey  
Sent: Wednesday, February 14, 2007 10:24 AM  
To: removed\_email\_address@domain.invalid  
Subject: [Rails] Re: RESTful design/url question

you can use the builtin helper:

edit\_order\_path(@customer,@order)

that will create a link like “/customers/1/order/1;edit”

new\_order\_path(@customer) will create a link to “/customer/1/order/new”

ed

On 2/14/07, Meech [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

Thanks a bunch.

Follow-up question if you don’t mind…

You are on the page to view a customers orders:  
/customers/1/orders/

User clicks edit, Would you replicate the edit, ala:

/customers/1/orders/1;edit

Or kick 'em to the orders resource?

/orders/1;edit

I’d like to be able to click “New Order” from within the customer  
view,  
something like

/customers/1/orders/new

But the replication doesn’t feel very DRY.

-----Original Message-----  
From: removed\_email\_address@domain.invalid  
[[mailto:removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
[mailto:removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) ] On Behalf Of Jeff  
Sent: Wednesday, February 14, 2007 9:43 AM  
To: Ruby on Rails: Talk  
Subject: [Rails] Re: RESTful design/url question

On Feb 14, 8:25 am, “Meech” \< removed\_email\_address@domain.invalid  
[mailto:removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) \> wrote:

> New to rails and restful design, forgive me if this has been  
> asked…

Not at all.

> Lets say you have customers and orders models. orders  
> belongs\_to  
> customers.
> 
> When viewing a customer, (/customers/1/) I want to present the  
> option  
> to view the customers orders (and eventually invoices, POs, etc).  
> What route would be within restful guidelines?
> 
> /customers/1:orders  
> /customers/1?view=orders  
> /customers/1/orders/  
> /orders:filter?customer=1

I believe it would be /customers/1/orders/ . You achieve this by  
specifying  
that orders are nested within customers in your routes.rb:

map. resources :customers do |customer|  
customer.resources :orders  
end

and then, in your OrdersController class, use a before\_filter to  
identify  
the customer, something like:

before\_filter :get\_customer

def get\_customer  
@customer = Customer.find(params[:customer\_id])  
end

The customer\_id key is automatically created for you when you nest  
the  
routes.

> I think I would prefer each “page” of a customer detail have it’s  
> own  
> controller method – this will allow me to keep the security  
> decisions  
> completely within the controller easily.

That’s how Rails works anyway - each “page” is an “action”. An  
action is a  
coupling of a controller method and a template (.rhtml page). So  
you’re on  
the right track.

Hope this helps  
Jeff  
[softiesonrails.com](http://softiesonrails.com)

---

<div class="post-metadata">

**Author:** ![Meech](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/0ea827/32.png) [@Meech](https://www.ruby-forum.com/u/Meech)\
**Post date:** [February 14, 2007, 6:04pm UTC](https://www.ruby-forum.com/t/restful-design-url-question/85554/6 "2007-02-14T18:04:01Z")

</div>

On 2/14/07, Meech [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> So, if we are viewing a customer/orders page, /customers/1/orders/ and  
> they should return after completing the edit.  
> Meech,

Why do you want to expose orders as a first level resource  
(/orders/1)? If an order is always associated with a customer then  
the nested way (/customers/1/orders/1) makes more sense.

What is it you’re trying to accomplish by exposing orders via two  
resource paths?

–  
Zack C.  
[http://depixelate.com](http://depixelate.com)

---

<div class="post-metadata">

**Author:** ![Meech](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/0ea827/32.png) [@Meech](https://www.ruby-forum.com/u/Meech)\
**Post date:** [February 14, 2007, 6:22pm UTC](https://www.ruby-forum.com/t/restful-design-url-question/85554/7 "2007-02-14T18:22:06Z")

</div>

It depends on your model associations. If Order belongs\_to Customer,  
then I  
would go the “/customers/1/orders/new” because you’ll need to reference  
to  
the Customer to create the Order.  
You can use nested routes to acheive this. Jamis B. recently posted  
about  
something related to your question:  
[http://weblog.jamisbuck.org/2007/2/5/nesting-resources](http://weblog.jamisbuck.org/2007/2/5/nesting-resources)

ed

---

<div class="post-metadata">

**Author:** ![Meech](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/0ea827/32.png) [@Meech](https://www.ruby-forum.com/u/Meech)\
**Post date:** [February 14, 2007, 6:29pm UTC](https://www.ruby-forum.com/t/restful-design-url-question/85554/8 "2007-02-14T18:29:06Z")

</div>

It just seems nicer to be able to view open/active orders across  
customers to perform order-related functions. The user could goto:

/orders/?q=active

Click on the order and manipulate it, instead of remembering the  
customer, clicking on the orders tab, then clicking on the order.

But lets take this another level deep. Orders have invoices,  
invoices has payments.

customer  
–order  
----invoice  
------payment

A person in accounts receivable would like to see unpaid invoices (as  
a whole, not specific to a customer) in order to perform collection  
activities. Or be able to see payments within a deposit instead of  
by customer/invoice, etc, etc…

While somebody can view invoices via /customer/1/invoice/1/ how  
would they see all open invoices for the company? via: /invoices/

This is why I posed the original question. Is it restfull to  
duplicate functions like this?

Is it?  
/customers/1/invoices  
/invoices/customers/1/  
/invoices/?customer=1

---

<div class="post-metadata">

**Author:** ![Meech](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/0ea827/32.png) [@Meech](https://www.ruby-forum.com/u/Meech)\
**Post date:** [February 14, 2007, 11:28pm UTC](https://www.ruby-forum.com/t/restful-design-url-question/85554/9 "2007-02-14T23:28:52Z")

</div>

On 2/14/07, Meech [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> invoices has payments.
> 
> /invoices/?customer=1  
> I understand where you’re coming from now. I think you could do this  
> a few ways. One is to just expose the additional route in routes.rb

map. resources :customers do |customer|  
customer.resources :orders  
end  
map.admin\_orders ‘/orders’, :controller =\> ‘orders’

You can also make an admin controller for handling workflow types of  
things like order approval (in non-REST fashion). Or you can view  
status changing of orders (aka pending, verified, charged, shipped,  
etc) as an OrderStatus resource and expose this via a REST controller.

Good luck.

–  
Zack C.  
[http://depixelate.com](http://depixelate.com)
