2013-03-06 62 views
0

更新#2 我已經得到它的工作,但我怎樣才能讓它計數每個刷新?即使我刷新用戶的個人資料(例如/ users/3),我也希望它能夠計數。使用「印象派」寶石(Ruby on Rails)計算頁面瀏覽量時遇到問題

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 當我做出改變賽義德的回答下面說明,並嘗試在「部件」控制器「窗口小部件」模塊,現在我得到這個錯誤:

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+\-.][email protected][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並運行了數據庫遷移。

然後,我創建了一個新的「部件」控制器應用程序\控制器文件

WidgetsController < ApplicationController 

    def show 
    @widget = Widget.find 
    impressionist(@widget) 
    end 
end 

然後我創建了應用程序/模型創建一個新「窗口小部件」的模式

class Widget < ActiveRecord::Base 
    is_impressionable 
end 

然後我說

<%= @widget.impressionist_count %> 

show.html.erb查看

我想要統計的是用戶的配置文件視圖的數量。在整個網站中,您可以點擊一個用戶名,並將其放入他們的個人資料中。我只想顯示點擊他們個人資料的次數。

感謝

回答

2

看來問題是在表演的動作,而不是 :

@widget = Widget.find 

試試這個

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

更新:1

你應該添加is_impressionable to your user model

+0

這些代碼是否進入「Widgets」控制器和「Widget」模型?或「用戶」控制器和「用戶」模型?當我用「Widgets」進行嘗試時,我得到:用戶中的NoMethodError#顯示未定義的方法'impressionist_count'爲nil:NilClass。這使得我在「用戶」中嘗試它,但它給了我這個錯誤:UsersController中的ActiveRecord :: StatementInvalid#show 找不到表'小部件 – syk 2013-03-06 06:46:47

+0

它在小部件控制器 – 2013-03-06 07:11:07

+0

好的謝謝澄清。我得到這個錯誤信息:未定義的方法'impressionist_count'爲nil:NilClass。你知道這可能是什麼原因嗎? – syk 2013-03-06 07:17:42