【问题标题】:Acts As Votable Get Upvotes Error充当可投票者获得赞成票错误
【发布时间】:2015-05-20 00:28:25
【问题描述】:

在我的 rails 4 应用程序中使用 Acts as Votable gem。

到目前为止,我已经完成了所有工作,除了调用 get_upvotes 和 get_downvotes 来显示帖子有多少票时,我收到了这个错误:

undefined method `get_upvotes' for #<Post:0x000001078b5f58>

这是我的帖子视图 (_post.html.erb):

  <%= post.name %>
  <%= post.title %>
  <%= post.content %><br>
  <%= link_to 'Show', post %>
  <% if can? :update, @post %>
    <%= link_to 'Edit', edit_post_path(post) %>
  <% end %>
  <% if can? :destroy, @post %>
    <%= link_to 'Destroy', post, method: :delete, data: { confirm: 'Are you sure?' } %>
  <% end %>

  <%= link_to like_post_path(post), class: "like", method: :put do %>
    <button type="button" class="btn btn-info" aria-label="Left Align">
      <span class="glyphicon glyphicon-thumbs-up glyphicon-align-center" aria-hidden="true"></span>
      <span class="badge"><%= post.get_upvotes.size %></span>
    </button>
  <% end %>

  <%= link_to dislike_post_path(post), class: "like", method: :put do %>
    <button type="button" class="btn btn-info" aria-label="Left Align">
      <span class="glyphicon glyphicon-thumbs-down glyphicon-align-center" aria-hidden="true"></span>
      <span class="badge"><%= post.get_downvotes.size %></span>
    </button>
  <% end %>

这是我的控制器 (posts_controller.rb):

class PostsController < ApplicationController
  load_and_authorize_resource
  before_action :set_post, only: [:show, :edit, :update, :destroy, :upvote, :downvote]

  def index
  end

  def show
  end

  def new
  end

  def edit
  end

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

  def update
    respond_to do |format|
      if @post.update(post_params)
        format.html { redirect_to @post, notice: 'Post was successfully updated.' }
        format.json { render :show, status: :ok, location: @post }
      else
        format.html { render :edit }
        format.json { render json: @post.errors, status: :unprocessable_entity }
      end
    end
  end

  def destroy
    @post.destroy
    respond_to do |format|
      format.html { redirect_to posts_url, notice: 'Post was successfully destroyed.' }
      format.json { head :no_content }
    end
  end

  def upvote
    @post = Post.find(params[:id])
    @post.upvote_from current_user
    redirect_to :back
  end

  def downvote
    @post = Post.find(params[:id])
    @post.downvote_from current_user
    redirect_to :back
  end

  private

    def set_post
      @post = Post.find(params[:id])
    end

    def post_params
      params.require(:post).permit(:name, :title, :content, :image, :image2, :video1, :video2)
    end
end

和我的模型(post.rb):

class Post < ActiveRecord::Base
  acts_as_voter
  belongs_to :user
end

用户模型(user.rb):

class User < ActiveRecord::Base
  acts_as_votable
  has_many :posts
end

最后是我的路线(routes.rb):

resources :posts do
  member do
    put "like" => "posts#upvote"
    put "dislike" => "posts#downvote"
  end
end

有谁知道为什么 get_upvotes 不起作用?

【问题讨论】:

    标签: ruby-on-rails ruby ruby-on-rails-4 error-handling vote


    【解决方案1】:

    阅读文档,看起来get_upvotesacts_as_votable 上的方法,而不是acts_as_voter

    我不太了解您的代码上下文,但User 不应该对Post 进行投票,如下所示:

    class Post < ActiveRecord::Base
      acts_as_votable
      belongs_to :user
    end
    

    用户模型:

    class User < ActiveRecord::Base
      acts_as_voter
      has_many :posts
    end
    

    见:https://github.com/ryanto/acts_as_votable#votable-models

    【讨论】:

    • 哇,我觉得自己很笨。我已经被困了好几个小时,整个时间只需要换个联想。谢谢!
    猜你喜欢
    • 1970-01-01
    • 2014-02-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-02-25
    • 1970-01-01
    相关资源
    最近更新 更多