Ruby on Rails。尝试通过嵌套注释上的acts_as_votable gem添加投票功能时出现各种错误。
acts_as_votable适用于帖子,但不适用于帖子下的嵌套评论。
使用:
<%= link_to "Like", like_post_comment_path(@post, @comment), method: :put do %>
<i class="fa fa-arrow-up" aria-hidden="true">Yes</i>
<%= @p.get_upvotes.size %>
<% end %>
我收到错误: 没有路由匹配{:action =&gt;“upvote”,:controller =&gt;“comments”,:id =&gt; nil,:post_id =&gt;“1”},缺少必需的键:[:id]
使用:
<%= link_to "Like", like_post_comment_path(@post, @comment.id), method: :put do %>
<i class="fa fa-arrow-up" aria-hidden="true">Yes</i>
<%= @p.get_upvotes.size %>
<% end %>
我收到错误: nil的未定义方法`id':NilClass
我尝试过各种组合作为参数。都会产生错误。
我的评论控制器:
class CommentsController < ApplicationController
before_action :find_post, only: [:upvote, :downvote, :create, :destroy, :edit, :update]
before_action :find_comment, only: [:upvote, :downvote :destroy, :update, :edit, :comment_owner ]
before_action :comment_owner, only: [:destroy, :edit, :update]
def new
@comment = Comment.new
@post = Post.new
end
def create
@comment = @post.comments.create(params[:comment].permit(:content))
@comment.user_id = current_user.id
@comment.save
if @comment.save
redirect_back(fallback_location: root_path)
else
render 'new'
end
end
def destroy
@comment.destroy
redirect_back(fallback_location: root_path)
end
def edit
@comment = Comment.find(params[:id])
@p = Post.find(params[:post_id])
end
def update
if @comment.update(params[:comment].permit(:content))
respond_to do |f|
if (@comment.save)
f.html { redirect_to "/explore", notice: "Comment edited!" }
elsif
f.html { redirect_to "/explore", notice: "Error: Your Comment is the same!." }
else
render 'edit'
#f.html { render 'edit'} ## Specify the format in which you are rendering "new" page
end
end
end
end
def upvote
@comment.upvote_from current_user
redirect_back(fallback_location: root_path)
end
def downvote
@comment.downvote_from current_user
redirect_back(fallback_location: root_path)
end
private
def find_post
@post = Post.find(params[:post_id])
end
def find_comment
@comment = @post.comments.find(params[:id])
end
def comment_owner
unless current_user.id == @comment.user_id
flash[:notice] = "You can't do that!"
redirect_back(fallback_location: root_path)
end
end
end
评论模型:
class Comment < ApplicationRecord
belongs_to :commentable, :polymorphic => true
belongs_to :user
acts_as_votable
# scope :desc
default_scope { order(created_at: :desc) }
end
发布模型:
class Post < ApplicationRecord
belongs_to :user
has_many :comments, :as => :commentable
attr_accessor :post_id
acts_as_votable
validates :user_id, presence: true
validates :content, presence: true, length: { maximum: 100} # Questions are capped at 100 chars.
default_scope { order(cached_votes_score: :DESC) }
end
User model:
class User < ApplicationRecord
has_many :posts
has_many :comments, :as => :commentable
acts_as_voter
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
has_many :posts, dependent: :destroy
def user
user = User.find_by(params[:id])
end
like_post_comment PUT /posts/:post_id/comments/:id/like(.:format)comments#upvote
dislike_post_comment PUT /posts/:post_id/comments/:id/dislike(.:format)comments#downvote