更新#2 我已经有了它的工作但是如何让它计算每次刷新?即使我刷新用户的个人资料(例如/ users / 3),我也希望它能够计算。
Widget.rb
class Widget < ActiveRecord::Base
is_impressionable
def impressionist_count
impressions.size
end
end
小部件控制器
WidgetsController < ApplicationController
def show
@widget = Widget.find(params[:id])
impressionist(@widget,message:"wtf is a widget?") #message is optional
end
end
将Is_Impressionable添加到用户模型
这是我用于show.html.erb视图的代码
<%= @user.impressionist_count(:filter=>:all) %>
更新#1 当我在Said的回答中做出如下所述的更改并在“Widgets”控制器和“Widget”模块中尝试时,我现在收到此错误:
NoMethodError in Users#show
undefined method `impressionist_count' for nil:NilClass
这是user.rb
class User < ActiveRecord::Base
attr_accessible :name, :email, :password, :password_confirmation
has_secure_password
has_many :microposts, dependent: :destroy
has_many :impressions, :as=>:impressionable
has_many :relationships, foreign_key: "follower_id", dependent: :destroy
has_many :followed_users, through: :relationships, source: :followed
has_many :reverse_relationships, foreign_key: "followed_id",
class_name: "Relationship",
dependent: :destroy
has_many :followers, through: :reverse_relationships, source: :follower
before_save { |user| user.email = user.email.downcase }
before_save :create_remember_token
validates :name, presence: true, length: { maximum: 50 }
VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i
validates :email, presence: true, format: { with: VALID_EMAIL_REGEX },
uniqueness: { case_sensitive: false }
validates :password, length: { minimum: 6 }
validates :password_confirmation, presence: true
def following?(other_user)
relationships.find_by_followed_id(other_user.id)
end
def follow!(other_user)
relationships.create!(followed_id: other_user.id)
end
def unfollow!(other_user)
relationships.find_by_followed_id(other_user.id).destroy
end
def feed
Micropost.from_users_followed_by(self)
end
def impressionist_count
impressions.size
end
private
def create_remember_token
self.remember_token = SecureRandom.urlsafe_base64
end
end
这是用户控制器
class UsersController < ApplicationController
before_filter :signed_in_user,
only: [:index, :edit, :update, :destroy, :following, :followers]
before_filter :correct_user, only: [:edit, :update]
before_filter :admin_user, only: :destroy
def index
@users = User.paginate(page: params[:page]).all
end
def show
@user = User.find(params[:id])
@microposts = @user.microposts.paginate(page: params[:page])
end
def new
@user = User.new
end
def create
@user = User.new(params[:user])
if @user.save
sign_in @user
flash[:success] = "Demoapp!"
redirect_to root_url
else
render 'new'
end
end
def edit
end
def update
if @user.update_attributes(params[:user])
sign_in @user
flash[:success] = "Profile updated"
redirect_to @user
else
render 'edit'
end
end
def destroy
User.find(params[:id]).destroy
flash[:success] = "User destroyed"
redirect_to users_path
end
def following
@title = "Follow"
@user = User.find(params[:id])
@users = @user.followed_users.paginate(page: params[:page])
render 'show_follow'
end
def followers
@title = "Following"
@user = User.find(params[:id])
@users = @user.followers.paginate(page: params[:page])
render 'show_follow'
end
private
def correct_user
@user = User.find(params[:id])
redirect_to root_path unless current_user?(@user)
end
def admin_user
redirect_to root_path unless current_user.admin?
end
end
原帖 我在下面做错了什么?
我添加了gem并运行了db迁移。
然后我在app \ controllers
中创建了一个新的“Widgets”控制器文件WidgetsController < ApplicationController
def show
@widget = Widget.find
impressionist(@widget)
end
end
然后我在app / models
中创建了一个新的“Widget”模型class Widget < ActiveRecord::Base
is_impressionable
end
然后我添加了
<%= @widget.impressionist_count %>
show.html.erb 视图中的
我想要计算的是用户个人资料视图的数量。在整个网站中,您可以点击用户名,它将进入他们的个人资料。我只是想显示点击他们的个人资料的次数。
由于
答案 0 :(得分:2)
似乎问题出在show action中, 代替:
@widget = Widget.find
试试这个
@widget = Widget.find(params[:id])
更新:1
您应该将 is_impressionable 添加到您的用户模型