喜欢评论时如何设置id?

时间:2015-06-09 04:26:54

标签: ruby-on-rails ruby model-view-controller

如果喜欢评论,怎样才能让这个comment_like_id: nil,不为零?

我在评论/评论中玩弄了这一行:

<%= link_to like_comment_path(:id => comment.id), class: "btn", method: :post do %>
  <span class='glyphicon glyphicon-thumbs-up'></span> Like
<% end %>

但是用comment_like_id替换行最终会给我带来其他错误。

模型

class Comment < ActiveRecord::Base
  has_many :comment_likes   
  has_many :likers, through: :comment_likes, class_name: 'User', source: :liker
  belongs_to :user
end

class CommentLike < ActiveRecord::Base
  belongs_to :comment
  belongs_to :user
  validates :user, uniqueness: { scope: :comment }
  belongs_to :liker, class_name: 'User', foreign_key: :user_id
  belongs_to :liked_comment, class_name: 'Comment', foreign_key: :comment_id
end

comments_controller

class CommentsController < ApplicationController
  before_action :set_commentable, only: [:index, :new, :create]
  before_action :set_comment, only: [:edit, :update, :destroy, :like]

  def index
    @comments = @commentable.comments
  end

  def new
    @comment = @commentable.comments.new
  end

  def create
    @comment = @commentable.comments.new(comment_params)
    if @comment.save
      redirect_to @commentable, notice: "Comment created."
    else
      render :new
    end
  end

  def edit
  end

  def update
    if @comment.update_attributes(comment_params)
      redirect_to :back, notice: "Comment was updated."
    else
      render :edit
    end
  end

  def destroy
    @comment.destroy
    redirect_to @comment.commentable, notice: "Comment destroyed."
  end

  def like
    @comment_like = current_user.comment_likes.build(comment: @comment)
    if @comment_like.save
      @comment.increment!(:likes)
      flash[:success] = 'Thanks for liking!'
    else
      flash[:error] = 'Too many likes'
    end  
    redirect_to(:back)
  end

  private

  def set_commentable
    @commentable = find_commentable
  end

  def set_comment
    @comment = current_user.comments.find(params[:id])
  end

  def find_commentable
    if params[:goal_id]
      Goal.find(params[:goal_id])
    elsif params[:habit_id]
      Habit.find(params[:habit_id])
    elsif params[:valuation_id]
      Valuation.find(params[:valuation_id])
    elsif params[:quantified_id]
      Quantified.find(params[:quantified_id])
    end
  end

  def comment_params
    params[:comment][:user_id] = current_user.id
    params.require(:comment).permit(:content, :commentable, :user_id, :like)
  end
end

模式

  create_table "comment_likes", force: true do |t|
    t.integer  "user_id"
    t.integer  "comment_id"
    t.integer  "commentable_id"
    t.string   "commentable_type"
    t.integer  "likes"
    t.datetime "created_at",       null: false
    t.datetime "updated_at",       null: false
  end

  create_table "comments", force: true do |t|
    t.text     "content"
    t.integer  "goal_id"
    t.integer  "habit_id"
    t.integer  "valuation_id"
    t.integer  "quantified_id"
    t.integer  "commentable_id"
    t.string   "commentable_type"
    t.integer  "user_id"
    t.datetime "created_at",       null: false
    t.datetime "updated_at",       null: false
    t.integer  "likes"
  end

如果您需要进一步的说明或代码来帮助我,请告诉我们:)

1 个答案:

答案 0 :(得分:0)

我需要将comment_like_id添加到comments表和comments.rb的create操作中。我意识到它只是在通知下才会产生问题。这有助于了解您是否使用本教程:http://evanamccullough.com/2014/11/ruby-on-rails-simple-notifications-system-tutorial/