REST doubt

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..................

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 Tigner

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.

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)
%></td>

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

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.

Brilliant Thanks Yar....

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

Hi,

There is a pdf about RESTful Rails:

http://www.b-simple.de/documents

Kind regards, Erik