gpt4 book ai didi

ruby-on-rails - 充当Voable获取Upvotes错误

转载 作者:行者123 更新时间:2023-12-03 08:12:53 26 4
gpt4 key购买 nike

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

到目前为止,我已经使所有工作正常,但当我调用get_upvotes和get_downvotes来显示帖子拥有多少票时,出现此错误:

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

这是我的帖 subview (_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 %>

这是我的 Controller (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不起作用?

最佳答案

阅读文档,看来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

关于ruby-on-rails - 充当Voable获取Upvotes错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30338305/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com