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.