我想在我的rails應用程序的帖子和評論中使用acts_as_votable
投票系統。我目前產生了我comments#upvote
和comments#downvote
一些明顯不當的路線,在這裏,他們都低於:添加acts_as_votable投票到帖子和評論,路由問題?
upvote_post PUT /posts/:id/upvote(.:format) comments#upvote
downvote_post PUT /posts/:id/downvote(.:format) comments#downvote
但他們路由都需要像/posts/comment/:id/downvote
。這裏是我目前如何做我的路線
resources :posts do
member do
put "like", to: "posts#upvote"
put "dislike", to: "posts#downvote"
end
resources :comments
member do
put "upvote", to: "comments#upvote"
put "downvote", to: "comments#downvote"
end
end
此外,我需要兩張選票表,因爲我希望這兩個評論和帖子可投票? 這裏是我的我的意見控制器如果需要的話:
class CommentsController < ApplicationController
before_filter :authenticate_user!, :except => [:index, :show]
def index
@post = Post.find(params[:post_id])
@user = User.find(params[:user_id])
@comments = @post.comments.order('created_at desc')
end
def new
@post = Post.find(params[:post_id])
@comment = @post.comments.new(params[:id])
end
def create
@post = Post.find(params[:post_id])
@comment = @post.comments.create(comment_params)
@comment.post_id = @post.id
@comment.user_id = current_user.id
if @comment.save
redirect_to post_comments_path(@post)
else
redirect_to new_post_comment_path(post)
end
end
def destroy
end
def upvote
@post = Post.find(params[:post_id])
@comment = @post.comments.find(params[:id])
@comment.upvote_by current_user
redirect_to post_comments_path(@post)
end
def downvote
@post = Post.find(params[:post_id])
@comment = @post.comments.find(params[:id])
@comment.downvote_by current_user
redirect_to post_comments_path(@post)
end
private
def comment_params
params.require(:comment).permit(:body, :post_id, :user_id)
end
end
感謝您的幫助
什麼是發現模型的方法做的,我真的用這個詞模型? – 2014-11-05 14:27:26