我是 Rails 新手,有一个棘手的路由问题。我有一个嵌套集合的资源,它映射到嵌套集合类型的控制器。但是,集合的名称不会映射到我要将请求路由到的控制器。
基本上用户模型有很多追随者和追随者。这 2 个集合映射到 Follow 模型,该模型具有使用标准 crud 方法的跟随控制器。
用户.rb:
class User < ActiveRecord::Base
has_many :followers, class_name: Follow, foreign_key: :user_id, dependent: :destroy
has_many :followings, class_name: Follow, foreign_key: :follower_id, dependent: :destroy
end
跟随.rb:
class Follow < ActiveRecord::Base
belongs_to :user, counter_cache: :followers_count
belongs_to :follower, class_name: 'User', counter_cache: :followings_count
attr_accessible :user, :user_id, :follower, :follower_id
validates_presence_of :user_id, :follower_id
validates_uniqueness_of :user_id, scope: :follower_id
end
follow_controller.rb:
def index
respond_with @follows
end
路线.rb:
resources :users do
resources :followers, :controller => "follows"
resources :following, :controller => "follows"
end
耙路线:
api_v1_user_followers GET /api/v1/users/:user_id/followers(.:format) api/v1/follows#index
POST /api/v1/users/:user_id/followers(.:format) api/v1/follows#create
new_api_v1_user_follower GET /api/v1/users/:user_id/followers/new(.:format) api/v1/follows#new
edit_api_v1_user_follower GET /api/v1/users/:user_id/followers/:id/edit(.:format) api/v1/follows#edit
api_v1_user_follower GET /api/v1/users/:user_id/followers/:id(.:format) api/v1/follows#show
PUT /api/v1/users/:user_id/followers/:id(.:format) api/v1/follows#update
DELETE /api/v1/users/:user_id/followers/:id(.:format) api/v1/follows#destroy
api_v1_user_following_index GET /api/v1/users/:user_id/following(.:format) api/v1/follows#index
POST /api/v1/users/:user_id/following(.:format) api/v1/follows#create
new_api_v1_user_following GET /api/v1/users/:user_id/following/new(.:format) api/v1/follows#new
edit_api_v1_user_following GET /api/v1/users/:user_id/following/:id/edit(.:format) api/v1/follows#edit
api_v1_user_following GET /api/v1/users/:user_id/following/:id(.:format) api/v1/follows#show
PUT /api/v1/users/:user_id/following/:id(.:format) api/v1/follows#update
DELETE /api/v1/users/:user_id/following/:id(.:format) api/v1/follows#destroy
我尝试添加 :controller => 'follows' 并且确实将两个嵌套的集合路由正确映射到 follow_controller。但是我收到此错误:
Started GET "/api/v1/users/2/following?auth_token=qhcT6CSHi3S7KrEnJtq6" for 10.0.1.72 at 2013-04-22 09:52:43 -0600
ActionController::RoutingError (uninitialized constant Api::V1::FollowingController):
任何有关如何正确解决此问题的想法将不胜感激。
谢谢,