在Rails中获取一个帖子的点赞数



在我的rails应用程序中,我使用gem,社会化。我只是不知道如何显示点赞的数量!

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

我的用户控制器:

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

我的用户模型:

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

我的观点:

<% 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>

任何帮助都将非常感激!

First

has_many :like
has_many :likes

我想你的意思只是

has_many :likes

所以你可以输入

@likes_num = @post.likes.count

这将为喜欢的数量创建一个查询,而不是实际喜欢的数量。

老问题,但是我也找不到这个

正确的方法调用应该是:
post.likers(User).count

我仍然不确定为什么你必须传递模型名称,也许这样你就可以从喜欢的类中询问喜欢的数量?

相关内容

  • 没有找到相关文章

最新更新