如何在Ruby on Rails中为嵌套评论添加投票系统?

时间:2015-10-17 23:40:03

标签: ruby-on-rails ruby ruby-on-rails-3 ruby-on-rails-4

我试图使用Ruby gem“act_as_votable”设置一个投票系统(喜欢和不喜欢),但我想在我的评论模型中使用它。请记住,评论嵌套在我的应用程序中的帖子模型中。我试图在我的routes.rb文件中添加它,但它无法正常工作。有谁知道如何为嵌套模型添加投票系统?这是我的代码

的routes.rb

Rails.application.routes.draw do

  devise_for :users
    resources :posts do
    resources :reviews , except: [:show,:index]
    member do
      get "like" , to: "reviews#upvote"
      get "dislike" , to: "reviews#downvote"
    end

end


  get 'pages/help'

  get 'pages/blog'

  get 'pages/contact'
  get 'pages/tour'

  resources :posts
  root 'posts#index'
end

reviewsController.rb

class ReviewsController < ApplicationController
  before_action :set_review, only: [ :edit, :update, :destroy, :upvote,:downvote]
  before_action :set_post
  before_action :authenticate_user!

  respond_to :html



  def new
    @review = Review.new
    respond_with(@review)
  end

    def edit
      @review = Review.find(params[:id])
      @post = Post.find(params[:post_id])
    end


  def create
    @review = Review.new(review_params)
    @review.user_id = current_user.id
    @review.post_id = @post.id
    @review.save
    redirect_to post_path(@post)


  end

  def update
    @review.update(review_params)
    respond_with(@review)
  end

  def destroy
    @review.destroy
    respond_with(@review)
  end

  def upvote
    @review.upvote_bu current_user
    redirect_to :back
  end

  def downvote
    @review.downvote_bu current_user
    redirect_to :back
  end

  private
    def set_review
      @review = Review.find(params[:id])
    end

    def set_post
      unless @post = Post.where(id: params[:post_id]).first
        redirect_to posts_path, flash: {alert: "Post doesn't exists"}
      end
    end

    def review_params
      params.require(:review).permit(:comment)
    end
end

PostsController.rb

class PostsController < ApplicationController
  before_action :set_post, only: [:show, :edit, :update, :destroy]
  before_action :authenticate_user! , only: [:edit,:update,:destroy,:new]


  # GET /posts
  # GET /posts.json
  def index
    @posts = Post.all
  end

  # GET /posts/1
  # GET /posts/1.json
  def show
    @reviews = Review.where(post_id: @post.id)
  end

  # GET /posts/new
  def new
    @post = Post.new
  end

  # GET /posts/1/edit
  def edit
    @post = Post.find(params[:id])
  end

  # POST /posts
  # POST /posts.json
  def create
    @post = Post.new(post_params)

    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

  # PATCH/PUT /posts/1
  # PATCH/PUT /posts/1.json
  def update
    respond_to do |format|
      if @post.update(post_params)
        format.html { redirect_to root_path, 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

  # DELETE /posts/1
  # DELETE /posts/1.json
  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

  private
    # Use callbacks to share common setup or constraints between actions.
    def set_post
      @post = Post.find(params[:id])
    end

    # Never trust parameters from the scary internet, only allow the white list through.
    def post_params
      params.require(:post).permit(:title, :description)
    end
end

0 个答案:

没有答案