如何评论?

时间:2015-04-10 18:56:27

标签: ruby-on-rails ruby comments public-activity

使用http://www.sitepoint.com/activity-feeds-rails/为评估添加“喜欢”功能。在估值范围内#show人们可以评论。我希望人们也能喜欢评论。

我使用railscast来启用多态注释:http://railscasts.com/episodes/154-polymorphic-association-revised

_comments.html.erb

<% @comments.each do |comment| %>
  <%= User.find(comment.user_id).name %>
  <%= simple_format comment.content %> #Give User ability to like this content.

  <span class="label label-default"><%= pluralize(comment.likes, 'like') %></span>
  <%= link_to content_tag(:span, '', class: 'glyphicon glyphicon-thumbs-up') + ' Like it', method: :post %>
<% end %>

当我点击“赞”时,页面会从http://0.0.0.0:3000/valuations/3刷新到此http://0.0.0.0:3000/valuations/3?method=post,整数不会增加+1。

我在 _comments.html.erb 代码中尝试了20种不同的变体,所以我认为解决这个问题的方法更深入。

comments_controller

def like
  @comment.increment!(:likes)
  @comment.create_activity :like
  flash[:success] = 'Thanks for liking!'
end

模式

create_table "comments", force: true do |t|
  t.text     "content"
  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

的routes.rb

resources :comments do
 member do
  post :like
 end
end

附带问题是public_activity是实现facebook / twitter的最佳宝石,就像新闻源一样,人们可以评论和喜欢实际的Feed?我发现我将不得不对public_activity gem进行大量自定义,以实现许多应用程序创建者可能非常普遍的目标。

感谢您的时间和专业知识!

我的代码与提供的链接非常相​​似,但如果您需要更多代码或有疑问,请不要犹豫!

更新

valuations_controller

class ValuationsController < ApplicationController
  before_action :set_valuation, only: [:show, :edit, :update, :destroy, :like]
  before_action :logged_in_user, only: [:create, :destroy]

  def index
    if params[:tag]
      @valuations = Valuation.tagged_with(params[:tag])
    else
      @valuations = Valuation.order('RANDOM()')
    end
  end

  def show
    @valuation = Valuation.find(params[:id])
    @commentable = @valuation
    @comments = @commentable.comments
    @comment = Comment.new
  end

  def new
    @valuation = current_user.valuations.build
  end

  def edit
  end

  def create
    @valuation = current_user.valuations.build(valuation_params)
    if @valuation.save
      redirect_to @valuation, notice: 'Value was successfully created'
    else
      @feed_items = []
      render 'pages/home'
  end
end

  def update
    if @valuation.update(valuation_params)
      redirect_to @valuation, notice: 'Value was successfully updated'
    else
      render action: 'edit'
  end
end

  def destroy
    @valuation.destroy
    redirect_to valuations_url
  end

  def like
    without_tracking do
      @valuation.increment!(:likes)
    end
    @valuation.create_activity :like
    flash[:success] = 'Thanks for sharing your Value!'
    redirect_to valuation_path(@valuation)
  end

private

    def without_tracking
      Valuation.public_activity_off
      yield if block_given?
      Valuation.public_activity_on
    end

    def set_valuation
      @valuation = Valuation.find(params[:id])
    end

    def correct_user
      @valuation = current_user.valuations.find_by(id: params[:id])
      redirect_to valuations_path, notice: "Not authorized to edit this valuation" if @valuation.nil?
    end

    def valuation_params
      params.require(:valuation).permit(:name, :private_submit, :tag_list, :content, :commentable, :comment, :like)
    end
end

的routes.rb

Rails.application.routes.draw do

  get 'auth/:provider/callback', to: 'sessions#facebook'
  get 'auth/failure', to: redirect('/')
  get 'signout', to: 'sessions#destroy', as: 'signout'

  get 'password_resets/new'

  get 'password_resets/edit'

  resources :users do
    resources :comments
  end

  resources :habits do
    resources :comments
    resources :levels do
      # we'll use this route to increment and decrement the missed days
      resources :days_missed, only: [:create, :destroy]
    end
  end

  resources :goals do
    resources :comments
  end

  resources :valuations do
    resources :comments
    member do
      post :like
    end
  end

  resources :quantifieds do
    resources :comments
  end
  
  resources :results do
    resources :comments
  end

  resources :comments do
    resources :comments
    member do
      post :like
    end
  end

  resources :users

  resources :account_activations, only: [:edit]

  resources :activities

  resources :password_resets,     only: [:new, :create, :edit, :update]

  resources :relationships,       only: [:create, :destroy]

  get 'tags/:tag', to: 'pages#home', as: :tag

  resources :users do
    member do
      get :following, :followers
    end
  end

  get    'about'   => 'pages#about'
  get    'signup'  => 'users#new'
  get    'login'   => 'sessions#new'
  post   'login'   => 'sessions#create'
  delete 'logout'  => 'sessions#destroy'

  root 'pages#home'

2 个答案:

答案 0 :(得分:1)

我模拟你的代码,这有效:

在视图中更改此行:

<%= link_to content_tag(:span, '', class: 'glyphicon glyphicon-thumbs-up') + ' Like it', like_comment_path(:id => comment.id), method: :post %>

在控制器中:

def like
  @comment = Comment.find(params[:id])
  @comment.increment!(:likes)
  @comment.create_activity :like
  flash[:success] = 'Thanks for liking!'
  redirect_to valuation_path(:id => @comment.commentable_id) // If doesnt work -> redirect_to(:back)
end

在&。39资源之后的routes.rb中:评论做...&#39;

resources :comments

答案 1 :(得分:0)

您的link_to遗漏了应该发布的网址。它应该是这样的:

link_to content_tag(:span, '', class: 'glyphicon glyphicon-thumbs-up') + ' Like it', like_comment_path(comment), method: "post"

注意like_comment_path(comment)作为第二个参数。