2013-04-20 50 views
2

在我的Rails應用程序中,我使用了gem,socialization。 我只是不知道如何顯示喜歡的數量!Rails - 在帖子中獲得喜歡的次數

我交控制器:

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 

我樁模型:

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> 

任何幫助將不勝感激!

感謝, CoreyPizzle

回答

0

首先

has_many :like 
has_many :likes 

我想你的意思只是

has_many :likes 

所以,你會做

@likes_num = @post.likes.count 

哪樣Ç reate查詢喜歡的數量,而不是實際喜歡自己。

+0

nope,這只是說'未定義的方法喜歡' – coreypizzle 2013-04-20 18:16:51

+0

您是否刪除了has_many:like,that break rails grammar,you should have has_many:likes – rovermicrover 2013-04-20 19:00:08

+0

您的意思是讚的單個帖子還是所有帖子? – rovermicrover 2013-04-20 19:01:01

0

老問題,但我也找不到這個。

正確的方法調用似乎是:

post.likers(User).count 

我仍然不能確定你爲什麼要在通過型號名稱,也許這樣你就可以問喜歡從該類類似器的數量?

雖然這工作。

相關問題