0

更新 #2 我已经让它工作了,但我怎样才能让它每次刷新都计数?即使我刷新用户的个人资料(例如 /users/3),我也希望它计数。

小部件.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 并运行了数据库迁移。

然后我在 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视图中

我要计算的是用户个人资料视图的数量。在整个网站中,您可以单击用户名,它将进入他们的个人资料。我只想显示点击他们个人资料的次数的计数器。

谢谢

4

1 回答 1

2

似乎问题在于显示操作,而不是:

  @widget = Widget.find

试试这个

  @widget = Widget.find(params[:id])

更新:1

您应该将is_impressionable添加到您的用户模型中

于 2013-03-06T05:55:39.390 回答