Действует как голосующий Get Upvotes Error

Работа с жемчужиной Acts as Votable в моем приложении rails 4.

До сих пор у меня все работало, за исключением случаев, когда я вызываю 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?


person Kathan    schedule 20.05.2015    source источник


Ответы (1)


Читая документы, похоже, что get_upvotes - это метод acts_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.

person Jimmy Thompson    schedule 20.05.2015
comment
Вау, я чувствую себя тупым. Я застрял на несколько часов, и все это время мне просто нужно было переключить ассоциации. Спасибо! - person Kathan; 20.05.2015