syk
syk

Reputation: 221

Having trouble counting page views using "impressionist" gem (Ruby on Rails)

Update #2 I've got it working but how can I make it count each refresh? Even if I refresh on a user's profile (/users/3 for example), I want it to count.

Widget.rb

class Widget < ActiveRecord::Base
  is_impressionable

  def impressionist_count
    impressions.size
  end
  end

Widgets controller

WidgetsController < ApplicationController


def show
  @widget = Widget.find(params[:id])
  impressionist(@widget,message:"wtf is a widget?") #message is optional
end

 end

Added Is_Impressionable to the user model

and here's the code I'm using for show.html.erb view

<%= @user.impressionist_count(:filter=>:all) %>

Update #1 When I make the change noted below in Said's answer and try it in the "Widgets" controller and "Widget" module, I get this error now:

    NoMethodError in Users#show
undefined method `impressionist_count' for nil:NilClass

Here's the 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

and here's the Users controller

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

Original Post What did I do wrong below?

I added the gem and ran the db migration.

Then I created a new "Widgets" controller file in app\controllers

WidgetsController < ApplicationController

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

Then I created a new "Widget" model in app/models

class Widget < ActiveRecord::Base
  is_impressionable
end

Then I added

<%= @widget.impressionist_count %>

in the show.html.erb view

What I am trying to count is the # of user's profile views. Throughout the website, you can click on a username and it will go into their profile. I just want to show the counter of how many times were clicked into their profile.

Thanks

Upvotes: 0

Views: 2375

Answers (1)

Said Kaldybaev
Said Kaldybaev

Reputation: 9982

Seems the problem is in show action, instead:

  @widget = Widget.find

try this

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

UPDATE: 1

you should add is_impressionable to your user model

Upvotes: 2

Related Questions