1

我正在尝试在我的应用程序中实现类似于 Twitter 的关注者系统。我正在实现 Michael Hartl 的 Rails 教程中解释的 Follower 系统:

http://ruby.railstutorial.org/chapters/following-users

完成示例后,我注意到一个问题:

转到 /users/(id)/following 或 /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

附加信息:我正在使用设计来处理用户身份验证。

固定的:

问题是在用户控制器中放置以下和后续操作。

4

2 回答 2

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

于 2013-03-03T20:09:49.457 回答
1
resources :users do
  member do
 get  :following 
 get  :followers
    end

 end 

以上路线工作,并分别提供following_user_pathfollowers_user_path路线

于 2018-11-12T19:09:53.773 回答