# Implementing REST properly

**URL:** <https://discuss.rubyonrails.org/t/implementing-rest-properly/25582>\
**Category:** rubyonrails-talk\
**Created:** [February 28, 2008, 1:17pm UTC](https://discuss.rubyonrails.org/t/implementing-rest-properly/25582 "2008-02-28T13:17:13Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![11175](https://avatars.discourse-cdn.com/v4/letter/1/8edcca/32.png) [@11175](https://discuss.rubyonrails.org/u/11175)\
**Post date:** [February 28, 2008, 1:17pm UTC](https://discuss.rubyonrails.org/t/implementing-rest-properly/25582/1 "2008-02-28T13:17:13Z")

</div>

Hi,

Same question is in [Link\_to parameters ignored - Rails - Ruby-Forum](http://www.ruby-forum.com/topic/144125#new) but since it evolved from a different question I move it to its own topic.

---

<div class="post-metadata">

**Author:** ![11175](https://avatars.discourse-cdn.com/v4/letter/1/8edcca/32.png) [@11175](https://discuss.rubyonrails.org/u/11175)\
**Post date:** [February 28, 2008, 1:52pm UTC](https://discuss.rubyonrails.org/t/implementing-rest-properly/25582/2 "2008-02-28T13:52:23Z")

</div>

Hi again,

This is what I've done in the Programs index view:

\<%= link\_to 'View comments', :controller =\> 'comments', :action =\> 'index', :program\_name =\> program.program\_name %\>

And this in the comments controller index function:

# GET /comments # GET /comments.xml def index

if params[:program\_name] &nbsp;&nbsp;@comments = Comment.find(:all, :conditions =\> ["program\_name = ?", params[:program\_name]]) else &nbsp;&nbsp;@comments = Comment.find(params[:id]) end

respond\_to do |format| &nbsp;&nbsp;format.html # index.html.erb &nbsp;&nbsp;format.xml { render :xml =\> @comments } end

end

So do you thing that's the right way?

Cheers

---

<div class="post-metadata">

**Author:** ![nathan\_sharkey](https://avatars.discourse-cdn.com/v4/letter/n/e99b99/32.png) [@nathan\_sharkey](https://discuss.rubyonrails.org/u/nathan_sharkey)\
**Post date:** [February 28, 2008, 2:34pm UTC](https://discuss.rubyonrails.org/t/implementing-rest-properly/25582/3 "2008-02-28T14:34:00Z")

</div>

Hi,

My understanding of the RESTful way would lead me to using nested  
resources in this situation

so your urls would be

/Programs/1/comments for index of all comments  
for a program whose id is 1 /Programs/1/comments/new for new comments for a program  
whose id is 1 /Programs/1/comments/10/show for showing comment 10 for a  
program whose id is 1

and so on for all 7 crud actions. that way your comments controller  
does not need logic like that.

so in your routes.rb you would do this

map.resources :programs do |program| &nbsp;&nbsp;&nbsp;article.resources :comments end

which will build all those routes for you you can check the routes  
it creates by using

rake routes

from command line. it will list all of the routes that your  
routes.rb file sets up. very useful for finding the correct  
dynamic\_url

for instance a few from my routes rb are

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;company GET / companies/:id  
{:action=\>"show", :controller=\>"companies"} company\_profile GET /companies/:company\_id/ profile/:id {:action=\>"show", :controller=\>"profile"} &nbsp;&nbsp;new\_company GET /companies/ new  
{:action=\>"new", :controller=\>"companies"}

left hand column can be used by appending \_url or \_path accordingly

new\_company\_path in your index view for companies check your  
scaffolded views and controllers and you'll see them everywhere. the  
table in rake routes shows the mappings

hope this helps 🙂Nathan

---

<div class="post-metadata">

**Author:** ![11175](https://avatars.discourse-cdn.com/v4/letter/1/8edcca/32.png) [@11175](https://discuss.rubyonrails.org/u/11175)\
**Post date:** [February 28, 2008, 6:50pm UTC](https://discuss.rubyonrails.org/t/implementing-rest-properly/25582/4 "2008-02-28T18:50:11Z")

</div>

> hope this helps 🙂

Sure it does. Thanks!

---

<div class="post-metadata">

**Author:** ![11175](https://avatars.discourse-cdn.com/v4/letter/1/8edcca/32.png) [@11175](https://discuss.rubyonrails.org/u/11175)\
**Post date:** [February 29, 2008, 9:55pm UTC](https://discuss.rubyonrails.org/t/implementing-rest-properly/25582/5 "2008-02-29T21:55:56Z")

</div>

I've been now playing with this again. Sorry to be back with the same one.

Yeah the rake routes is great to see all options clearly. However I'm facing a problem:

This is what I have now in the routes:

&nbsp;&nbsp;map.resources :comments

&nbsp;&nbsp;map.resources :programs do |program| &nbsp;&nbsp;&nbsp;program.resources :comments &nbsp;&nbsp;end

Everthing is more or less working. I have created some programs and then using "/programs/:program\_id/comments/new" I have created some comments for programs.

But, according to routes using "program\_comments GET /programs/:program\_id/comments" should list all the comments for that program id.

However that always shows all the comments for all the programs. That is getting into the controller via the 'index' method which is:

&nbsp;&nbsp;# GET /comments &nbsp;&nbsp;# GET /comments.xml &nbsp;&nbsp;def index &nbsp;&nbsp;&nbsp;&nbsp;@comments = Comment.find(:all)

&nbsp;&nbsp;&nbsp;&nbsp;respond\_to do |format| &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;format.html # index.html.erb &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;format.xml { render :xml =\> @comments } &nbsp;&nbsp;&nbsp;&nbsp;end &nbsp;&nbsp;end

And that finds all the comments as the code is. So how can it find comments only for a give program with no changes in that controller?

> that way your comments controller does not need logic like that.

> map.resources :programs do |program| &nbsp;&nbsp;&nbsp;article.resources :comments end

I have changed article with programs in the code above. Is my routing still ok?

I must be doing something wrong again...

Thanks.

---

<div class="post-metadata">

**Author:** ![Mike\_Garey](https://avatars.discourse-cdn.com/v4/letter/m/a8b319/32.png) [@Mike\_Garey](https://discuss.rubyonrails.org/u/Mike_Garey)\
**Post date:** [February 29, 2008, 10:26pm UTC](https://discuss.rubyonrails.org/t/implementing-rest-properly/25582/6 "2008-02-29T22:26:06Z")

</div>

> 

\<snip\>

> However that always shows all the comments for all the programs. That is getting into the controller via the 'index' method which is:
> 
> &nbsp;&nbsp;# GET /comments &nbsp;&nbsp;# GET /comments.xml &nbsp;&nbsp;def index
> 
> &nbsp;&nbsp;&nbsp;&nbsp;@comments = Comment.find(:all)

\<snip\>

> And that finds all the comments as the code is. So how can it find comments only for a give program with no changes in that controller?

You need to change your controller so you scope the comments by the program, such as:

before\_filter :get\_program

private def get\_program @program = Program.find\_by\_id(params[:product\_id]) || Program.new end

public def index &nbsp;&nbsp;@comments = @program.comments end

Note that the above will only work when called in a nested fashion, such as /program/123/comments

If you try calling /comments, you'll get an empty list

Mike

---

<div class="post-metadata">

**Author:** ![Mike\_Garey](https://avatars.discourse-cdn.com/v4/letter/m/a8b319/32.png) [@Mike\_Garey](https://discuss.rubyonrails.org/u/Mike_Garey)\
**Post date:** [February 29, 2008, 10:31pm UTC](https://discuss.rubyonrails.org/t/implementing-rest-properly/25582/7 "2008-02-29T22:31:03Z")

</div>

just noticed that you have both a nested route and a non nested route for comments.

so if you want both '/comments' and '/programs/123/comments' to work, you'll need something like the following:

comments\_controller.rb

def index &nbsp;&nbsp;@program = Program.find\_by\_id(params[:program\_id]) if params[:program\_id] &nbsp;&nbsp;@comments = @program ? @program.comments : Comment.find(:all) end

Mike

---

<div class="post-metadata">

**Author:** ![nathan\_sharkey](https://avatars.discourse-cdn.com/v4/letter/n/e99b99/32.png) [@nathan\_sharkey](https://discuss.rubyonrails.org/u/nathan_sharkey)\
**Post date:** [March 1, 2008, 12:17am UTC](https://discuss.rubyonrails.org/t/implementing-rest-properly/25582/8 "2008-03-01T00:17:48Z")

</div>

sorry i probably mislead you by showing that route in my last mail as an example, i would get rid of the index action and show action instead use the program view and controller to display the comments in the context of the program, i can’t really envisage a situation where you would want to show all your comments in an index view. but i suppose you might want to have the index show a list of comments for a given program.

basically you would find the program based on params[:program\_id] and use it to populate @comments like so.

program = Program.find(params[:program\_id])

@comments = program.comments.find(:all)

given that other actions in the view will use program variable you could use a before\_filter to create @programs variable that can be referenced by all the actions. so for instance your new action can be done like this:-

def new

@program.comments.build

end

check out this tutorial, I confess i haven’t read it thoroughly but just skimming it i think it will explain all you will want to know.

[http://www.akitaonrails.com/2007/12/12/rolling-with-rails-2-0-the-first-full-tutorial](http://www.akitaonrails.com/2007/12/12/rolling-with-rails-2-0-the-first-full-tutorial)

cheers

nathan

---

<div class="post-metadata">

**Author:** ![nathan\_sharkey](https://avatars.discourse-cdn.com/v4/letter/n/e99b99/32.png) [@nathan\_sharkey](https://discuss.rubyonrails.org/u/nathan_sharkey)\
**Post date:** [March 1, 2008, 1:18am UTC](https://discuss.rubyonrails.org/t/implementing-rest-properly/25582/9 "2008-03-01T01:18:41Z")

</div>

sorry again just realised mike had replied already … 🙂 I don’t think you would actually want the comments controller to respond to just /comments so I would remove

map.resources :comments

from your routes.rb file.

cheers

---

<div class="post-metadata">

**Author:** ![11175](https://avatars.discourse-cdn.com/v4/letter/1/8edcca/32.png) [@11175](https://discuss.rubyonrails.org/u/11175)\
**Post date:** [March 4, 2008, 8:07pm UTC](https://discuss.rubyonrails.org/t/implementing-rest-properly/25582/10 "2008-03-04T20:07:10Z")

</div>

Hi, reply to myself (and anyone interested of course...)

> &nbsp;&nbsp;&nbsp;&nbsp;\<% form\_tag :action=\>"destroy", :controller=\>"session" do %\> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\<%= submit\_tag 'Log out' %\> &nbsp;&nbsp;&nbsp;&nbsp;\<% end %\>

So my problem what that the controller was not found in admin. So the fix is to use :controller=\>"/session"

That will look in root controller and that's where it is found.

Cheers!
