REST doubt

i am function in my controller like
def user_list


and i need to call this function in link_to method. and i written like
<%=link_to ‘User_list’,student,:method=>:get%>

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

Is any problem in routes.rb

please help…


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 [email protected]

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


But it showing error like

undefined method `departments_student_url’ for

what’s a problem?

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

with regards
babu nair

If you just pass student as the 2nd parameter in your link it will
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
You want member.

You should have a method called

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

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
You should have a deparments controller nested below students, with its
create (adding a department to a student), show, and delete (removing a
department) actions.

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

The departments controller looks at the student_id in the params (cos
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

This time the link would be

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

<%=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 <

Brilliant Thanks Yar…

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

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
are generated. The route names are listed there so you should see one
says departments_user (then you just need to add _url or _path when
make the method call in link_to.

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


There is a pdf about RESTful Rails:

Kind regards, Erik