我正在尝试将acts_as_follower gem添加到我正在开发的rails应用程序中,但我似乎总是得到一个异常错误,表示用户ID为nil。浏览我在Google上找到的每个教程,似乎都没有解决我的问题。
用户控制器:
class UsersController < ApplicationController
def follow
@user = User.find(params[:id])
current_user.follow(@user)
redirect_to :back
end
def unfollow
@user = User.find(params[:id])
current_user.stop_following(@user)
redirect_to :back
end
end
用户模型:
class User < ActiveRecord::Base
has_many :posts, dependent: :destroy
acts_as_follower
acts_as_followable
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
end
发布模型:
class Post < ActiveRecord::Base
belongs_to :user
mount_uploader :avatar, AvatarUploader
acts_as_followable
end
routes.rb中:
Rails.application.routes.draw do
resources :posts
devise_for :users
root 'page#index'
get '/users/:id' => 'page#profile'
get 'page/feed'
get 'page/home'
resources :users do
member do
get :follow
get :unfollow
end
end
end
我要添加到个人资料视图的链接:
<%= link_to "Follow", follow_user_path(@user)%>
我在路线上写错了吗?因为它在我在控制台中测试时起作用了。错误开始显示我试图让它真正为用户提供功能。