# REST doubt

**URL:** <https://www.ruby-forum.com/t/rest-doubt/144631>\
**Category:** Rails\
**Created:** [July 31, 2008, 5:19pm UTC](https://www.ruby-forum.com/t/rest-doubt/144631 "2008-07-31T17:19:46Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![srijith](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/c77e96/32.png) [@srijith](https://www.ruby-forum.com/u/srijith)\
**Post date:** [July 31, 2008, 5:19pm UTC](https://www.ruby-forum.com/t/rest-doubt/144631/1 "2008-07-31T17:19:46Z")

</div>

Hi  
i am function in my controller like  
def user\_list  
…  
end

and i need to call this function in link\_to method. and i written like  
this  
\<%=link\_to ‘User\_list’,student,:method=\>:get%\>

when i click the link action by default going to show method…wats a  
problem.  
i mentioned in routes.rb as  
map.resources :students, :collection =\> {:department =\> :get,:user\_list  
=\>:get}

Is any problem in routes.rb

please help…

---

<div class="post-metadata">

**Author:** ![srijith](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/c77e96/32.png) [@srijith](https://www.ruby-forum.com/u/srijith)\
**Post date:** [July 31, 2008, 5:29pm UTC](https://www.ruby-forum.com/t/rest-doubt/144631/2 "2008-07-31T17:29:32Z")

</div>

Greetings!

If you run the rake task: “rake routes”, you’ll be given a list of all  
the available routes. When you use collection, the original resource  
is also included in the named rout. For example, you would call:

\<%= link\_to ‘User\_list’, user\_list\_students\_path %\>

This ‘should’ generate the correct URL. Use that rake routes command  
to confirm this.

Brian is right though, you seldom have to resort to custom methods.  
Your index page for students should be used to list your students.

Hope that helps!

~Dustin T.

On Jul 31, 9:19 am, babu nair [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![srijith](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/c77e96/32.png) [@srijith](https://www.ruby-forum.com/u/srijith)\
**Post date:** [August 1, 2008, 6:47am UTC](https://www.ruby-forum.com/t/rest-doubt/144631/3 "2008-08-01T06:47:38Z")

</div>

Hi i given in routes.rb as u mentioned like

map.resources :students, :member =\> [:departments =\> :get]

and rhtml i given link

```
<td><%=link_to "Departments", departments_student_url(student) 

```

%\>

in students controller

def departments  
@student= Student.find params[:id]

# @departments = @student.departments

end

But it showing error like

undefined method `departments\_student\_url’ for  
#ActionView::Base:0xb73d2f18

what’s a problem?

please sujjest any good link to study RESTful architecture in detail and  
also in depth?

with regards  
babu nair

---

<div class="post-metadata">

**Author:** ![srijith](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/c77e96/32.png) [@srijith](https://www.ruby-forum.com/u/srijith)\
**Post date:** [July 31, 2008, 5:37pm UTC](https://www.ruby-forum.com/t/rest-doubt/144631/4 "2008-07-31T17:37:43Z")

</div>

If you just pass student as the 2nd parameter in your link it will  
generate  
the link to student/show. You need to sit down and read through the  
docs on  
routing, methods, collections, etc, and you might want to look at “The  
Rails Way” book’s section on routing and REST.

Your route is totally wrong. You can’t map controllers and actions using  
map.resources, and a collection is for many students, not for one  
student.  
You want member.

You should have a method called

def departments  
@student= Student.find params[:id]  
@departments = @student.departments  
end

map.resources :students, :member = \> [:departments =\> :get]

Then your link would be

\<%=link\_to “Departments”, departments\_student\_url(@student) %\>

which generates /students/25/departments

If that looks funny to you it’s cos you’re breaking the REST  
conventions.  
You should have a deparments controller nested below students, with its  
own  
create (adding a department to a student), show, and delete (removing a  
department) actions.

map.resources :students do |s|  
s.resources :departments  
end

The departments controller looks at the student\_id in the params (cos  
that’s  
where the user id is found with a nested route.

So in the departments controller, the index action would be

def index  
@user = User.find params[:student\_id]  
@departments = @user.departments  
end

This time the link would be

def departments  
@user = User.find params[:id]  
@departments = @user.departments  
end

\<%=link\_to “Departments”, student\_departments\_url(@student) %\>

which generates /students/25/departments

Hope that helps. You should either embrace RESTful Rails design  
concepts, or  
just stop using map.resources and go back to map.connect  
:controller/:action/:id and do your link\_to stuff that way like in  
“classic” Rails.

On Thu, Jul 31, 2008 at 10:19 AM, babu nair \<

---

<div class="post-metadata">

**Author:** ![srijith](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/c77e96/32.png) [@srijith](https://www.ruby-forum.com/u/srijith)\
**Post date:** [August 4, 2008, 7:25am UTC](https://www.ruby-forum.com/t/rest-doubt/144631/5 "2008-08-04T07:25:11Z")

</div>

Brilliant Thanks Yar…

can you sujjest any book or website for studying RESTful in detail and  
depth…

---

<div class="post-metadata">

**Author:** ![srijith](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/c77e96/32.png) [@srijith](https://www.ruby-forum.com/u/srijith)\
**Post date:** [August 1, 2008, 3:42pm UTC](https://www.ruby-forum.com/t/rest-doubt/144631/6 "2008-08-01T15:42:32Z")

</div>

Your route should be a hash, not an array. I may have made a typo in my  
example. You should use:

map.resources :students, :member =\> {:departments =\> :get} #  
curly-braces instead of square braces.

Really sorry about that.

Use the command ‘rake routes’ to see a list of all the available routes  
that  
are generated. The route names are listed there so you should see one  
that  
says departments\_user (then you just need to add \_url or \_path when  
you  
make the method call in link\_to.

On Thu, Jul 31, 2008 at 11:47 PM, babu nair \<

---

<div class="post-metadata">

**Author:** ![srijith](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/c77e96/32.png) [@srijith](https://www.ruby-forum.com/u/srijith)\
**Post date:** [August 4, 2008, 3:07pm UTC](https://www.ruby-forum.com/t/rest-doubt/144631/7 "2008-08-04T15:07:15Z")

</div>

Hi,

There is a pdf about RESTful Rails:

[http://www.b-simple.de/documents](http://www.b-simple.de/documents)

Kind regards, Erik
