如何点赞评论?
How to like comments?
使用 http://www.sitepoint.com/activity-feeds-rails/ 将 "Liking" 功能添加到估值中。在 valuations#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 %>
当我点击 "like" 时,页面从 http://0.0.0.0:3000/valuations/3 to this 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 最好的 gem 实现 facebook/twitter 像新闻源一样,人们可以在实际的源上发表评论和点赞?我发现我将不得不对 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'
您的 link_to
缺少 url 它应该 post 的内容。应该是这样的:
link_to content_tag(:span, '', class: 'glyphicon glyphicon-thumbs-up') + ' Like it', like_comment_path(comment), method: "post"
注意 like_comment_path(comment)
作为第二个参数。
我模拟了你的代码,这有效:
在视图中更改此行:
<%= 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
使用 http://www.sitepoint.com/activity-feeds-rails/ 将 "Liking" 功能添加到估值中。在 valuations#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 %>
当我点击 "like" 时,页面从 http://0.0.0.0:3000/valuations/3 to this 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 最好的 gem 实现 facebook/twitter 像新闻源一样,人们可以在实际的源上发表评论和点赞?我发现我将不得不对 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'
您的 link_to
缺少 url 它应该 post 的内容。应该是这样的:
link_to content_tag(:span, '', class: 'glyphicon glyphicon-thumbs-up') + ' Like it', like_comment_path(comment), method: "post"
注意 like_comment_path(comment)
作为第二个参数。
我模拟了你的代码,这有效:
在视图中更改此行:
<%= 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