coreypizzle
coreypizzle

Reputation: 239

Get count of likes on a post in Rails

In my rails application, I am using the gem, socialization. I just can't figure out how to display the amount of likes!

My post controller :

class PostsController < ApplicationController
  # GET /posts
  # GET /posts.json
  def index
    @posts = Post.all(:order => "created_at DESC")
    @posts_not_signed_in = Post.all(:order => "created_at DESC")

    @post = Post.new
    @users = User.all(:order => "created_at DESC")

    respond_to do |format|
      format.html # index.html.erb
      format.json { render json: @posts }
    end
  end

  def like
    post.liked_by current_user
    redirect_to root_path
  end

  # GET /posts/1
  # GET /posts/1.json

  def show
    redirect_to posts_path
  end

  # GET /posts/new
  # GET /posts/new.json
  def new
    @post = Post.new

    respond_to do |format|
      format.html # new.html.erb
      format.json { render json: @post }
    end
  end

  # GET /posts/1/edit
  def edit
    @post = Post.find(params[:id])
  end

  # POST /posts
  # POST /posts.json
  def create
    @post = current_user.posts.build(params[:post])

    respond_to do |format|
      if @post.save
        format.html { redirect_to @post, notice: 'Post was successfully created.' }
        format.json { render json: @post, status: :created, location: @post }
      else
        format.html { render action: "new" }
        format.json { render json: @post.errors, status: :unprocessable_entity }
      end
    end
  end

  # PUT /posts/1
  # PUT /posts/1.json
  def update
    @post = Post.find(params[:id])

    respond_to do |format|
      if @post.update_attributes(params[:post])
        format.html { redirect_to @post, notice: 'Post was successfully updated.' }
        format.json { head :no_content }
      else
        format.html { render action: "edit" }
        format.json { render json: @post.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /posts/1
  # DELETE /posts/1.json
  def destroy
    @post = Post.find(params[:id])
    @post.destroy

    respond_to do |format|
      format.html { redirect_to posts_url }
      format.json { head :no_content }
    end
  end
end

My user controller :

class UsersController < ApplicationController
  def index
@users = User.all

respond_to do |format|
  format.html # index.html.erb
  format.xml  { render :xml => @users }
end


end
  def show
    @user = User.find(params[:id])

    respond_to do |format|
      format.html # show.html.erb
      format.xml  { render :xml => @user }
    end
  end

  def follow
      @user = User.find(params[:id])
      current_user.toggle_follow!(params[:id])
      redirect_to root_path
  end

  def unfollow
    @user = User.find(params[:id])
    current_user.stop_following(@user)
    redirect_to root_path
  end

end

My post model :

class Post < ActiveRecord::Base
  attr_accessible :status, :author, :username, :id, :user_id, :user, :website, :bio, :skype, :dob, :age, :email, :motto, :follower, :followable, :votes
  belongs_to :user
  has_many :like
  has_many :likes


  validates :status, :presence => true

  acts_as_likeable
  acts_as_votable

end

My user model :

class User < ActiveRecord::Base

  # Include default devise modules. Others available are:
  # :token_authenticatable, :confirmable,
  # :lockable, :timeoutable and :omniauthable
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable

  # Setup accessible (or protected) attributes for your model
  attr_accessible :email, :password, :password_confirmation, :remember_me, :username, :user_id, :id, :website, :bio, :skype, :dob, :age, :motto, :follower, :followable

  has_many :posts

  acts_as_liker
  # attr_accessible :title, :body
end

My view :

<% if user_signed_in? %>
  <h1 id="welcome" class="nuvo">Welcome <%= current_user.username %>!</h1>
<% else %>
  <h1 id="welcome" class="nuvo">Log-In to make some posts!</h1>
<% end%>

<div class="follow-row">
  <div class="titan-users nuvo"><h2>BoomIt Users</h2></div>
  <div class="user-row nuvo"><%= link_to 'coreypizzle', user_path(1) %> - <%= link_to 'BoomBoard', dashboard_path(1) %></div>
    <% @users.each do |user| %>
      <div class="user-row nuvo"><%= link_to user.username, user_path(user.id) %> - <%= link_to 'BoomBoard', dashboard_path(user.id) %></div>
    <% end %>
</div>

<div class="statuses">
  <% if user_signed_in? %><div class="status-form"><%= render 'form' %></div><% end %>
  <% if user_signed_in? %>
  <% @posts.each do |post| %>
    <div class="post">
      <div class="tstamp">
        <%= image_tag avatar_url_small(post.user), :class => 'gravatar' %>
        <strong>Posted <%= time_ago_in_words(post.created_at) %> ago by <%= post.user.username %></strong>
      </div>
      <div class="status"><%= post.status %></div>
      <div class="likearea"><%= link_to 'BoomThis', 'posts/like', :class => 'wtxt nuvo' %> - <%= @post.likes.size %></div>
    </div>
  <% end %>
  <% else %>
    <% @posts_not_signed_in.each do |post| %>
    <div class="post">
      <div class="tstamp">
        <%= image_tag avatar_url_small(post.user), :class => 'gravatar' %>
        <strong>Posted <%= time_ago_in_words(post.created_at) %> ago by <%= post.user.username %></strong>
      </div>
      <div class="status"><%= post.status %></div>
    </div>
  <% end %>
  <% end %>
</div>

Any help would be greatly appreciated!

Upvotes: 2

Views: 2350

Answers (2)

Chris Valentine
Chris Valentine

Reputation: 1637

Old question, but i couldn't find this either.

The correct method call appears to be:

post.likers(User).count

I'm still unsure why you have to pass the model name in, perhaps so you can ask for the number of likes from that class of liker?

This works though.

Upvotes: 0

rovermicrover
rovermicrover

Reputation: 1453

First

has_many :like
has_many :likes

I think you mean just

has_many :likes

So you would do

@likes_num = @post.likes.count

Which will create a query for the number of likes, and not the actually likes themselves.

Upvotes: 0

Related Questions