简体   繁体   中英

rails 3 namespaced controller and routing

I am having problem with namespaced controller and routing. I have following namespaced route.

  namespace :institution do
    resources :students
  end

along with

  resources :students, :only => [] do
    resources :college_selections, :only =>[:index, :create, :update]
    resources :progress, :only => [:index] do
      collection {get 'compare'}
    end
    resources :marks
  end

it generated the following routes

       institution_students GET    /institution/students(.:format)                 {:action=>"index", :controller=>"institution/students"}
                            POST   /institution/students(.:format)                 {:action=>"create", :controller=>"institution/students"}
    new_institution_student GET    /institution/students/new(.:format)             {:action=>"new", :controller=>"institution/students"}
   edit_institution_student GET    /institution/students/:id/edit(.:format)        {:action=>"edit", :controller=>"institution/students"}
        institution_student GET    /institution/students/:id(.:format)             {:action=>"show", :controller=>"institution/students"}
                            PUT    /institution/students/:id(.:format)             {:action=>"update", :controller=>"institution/students"}
                            DELETE /institution/students/:id(.:format)             {:action=>"destroy", :controller=>"institution/students"}

I have a students controller inside institution directory in apps directory. And it has following structure.

class Institution::StudentsController < ApplicationController
  before_filter :require_login
  load_and_authorize_resource
..........
.........
end

Now when i tried to redirect to students show page with a link_to helper method as shown below

<%= link_to "show", institution_student_path(student) %> 

then it showed wired kind of link with a period(.) in it. Say the student id is 100 and it generated the routes like this

institution/students/4.100 

here 4 is current institution id i guess. Why is such routes being generated.

And when i click on show link it gave me error saying

Expected /home/gagan/projects/App_name/app/Institution/students_controller.rb to define StudentsController

What am i missing here.

Thanks in advance.

you should be using

<%= link_to "show", institution_student_path(institution, student) %>

or

<%= link_to "show", institution_student_path(student.institution, student) %>

if there is an association between student and institution.

Never mind I got the answer. Actually I was putting institution folder outside controller folder which was the reason that the controller inside this folder are not being recognized. Hence solved the problem.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM