我正在尝试在我的应用中实现类似于Twitter的追随者系统。我正在实施Michael Hartl的Rails教程中解释的Follower系统:
http://ruby.railstutorial.org/chapters/following-users
完成示例后我注意到了一个问题:
转到/ users /(id)/ follow或/ users /(id)/ followers会显示以下错误消息:
Unknown action
The action 'following' could not be found for UsersController
或
Unknown action
The action 'followers' could not be found for UsersController
我对这些错误感到困惑的是,我确实在UsersController中定义了这些操作:
def following
@title = "Following"
@user = User.find(params[:id])
@users = @user.followed_users.paginate(page: params[:page])
render 'show_follow'
end
def followers
@title = "Followers"
@user = User.find(params[:id])
@users = @user.followers.paginate(page: params[:page])
render 'show_follow'
end
以下是我的routes.rb文件以防万一:
AppName::Application.routes.draw do
#get "users/index"
#get "users/show"
authenticated :user do
root :to => 'home#index'
end
root :to => "home#index"
devise_for :users
resources :users do
member do
get :following, :followers
end
end
resources :works
resources :relationships, only: [:create, :destroy]
end
其他信息:我正在使用Devise处理用户身份验证。
FIXED:
问题是在用户控制器中放置了以下和后续操作。
答案 0 :(得分:1)
在config/routes.rb
中,成员操作必须是每行一个:
resources :users do
member do
get :following
get :followers
end
end
另见http://guides.rubyonrails.org/routing.html#adding-more-restful-actions
答案 1 :(得分:1)
resources :users do
member do
get :following
get :followers
end
end
以上路由有效,并分别提供following_user_path
和followers_user_path
路由