0

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

我使用 railscasts 来启用多态评论: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 种不同的变体,所以我认为解决这个问题更深层次。

评论控制器

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

路线.rb

resources :comments do
 member do
  post :like
 end
end

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

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

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

更新

估价控制器

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

路线.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'

4

2 回答 2

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

在 routes.rb 之后的 'resources :comments do...'

resources :comments
于 2015-04-11T00:06:14.647 回答
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)作为第二个参数。

于 2015-04-10T19:25:47.707 回答