0

Used http://www.sitepoint.com/activity-feeds-rails/ to add "Liking" feature to valuations. Within valuations#show people can comment. I want people to be able to also like comments.

I used railscasts to enable polymorphic comments: 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 %>

When I click "like" the page refreshes from http://0.0.0.0:3000/valuations/3 to this http://0.0.0.0:3000/valuations/3?method=post and the integer doesn't increase +1.

I tried 20 different variations in the _comments.html.erb code so I think solving this problem lies deeper.

comments_controller

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

schema

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

Side question is public_activity the best gem to achieve a facebook/twitter like newsfeed where one can comment and like on the actual feed? I find that I'm going to have to do a lot of customization to the public_activity gem to achieve what is probably a very common goal for many app creators.

Thank you for your time and expertise!

My code is very similar to the links provided, but if you need more code or have questions please don't hesitate!

UPDATE

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'
AnthonyGalli.com
  • 2,796
  • 5
  • 31
  • 80
  • The link to like could be a simple GET request, this way it will be easier to generate a `link_to` with default method GET (it would also solve your problem apprently) – MrYoshiji Apr 10 '15 at 19:30
  • @MrYoshiji I've been thinking about switching public_activity over to http://getstream.io/. Any experience? This might be easier for creating a twitter like flat newsfeed instead of taking 20 steps to customize public_activity. – AnthonyGalli.com Apr 10 '15 at 21:11

2 Answers2

1

I simulate your code, and this works:

In the view change this line:

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

And in the controller:

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

And in routes.rb after 'resources :comments do...'

resources :comments
Papaya Labs
  • 1,079
  • 9
  • 11
  • Thanks Manfergo! The likes are incrementing properly now, but when I hit like I am redirected to a new page that gives me this error: `Missing template comments/like` I figure that means i need to create a comments/like.html.erb file BUT I don't really want to redirect to a new page when liking something I want it to stay on the same page and just refresh to show the new like amount. Is this possible? – AnthonyGalli.com Apr 13 '15 at 19:35
  • Yes, but you have to redirect to the page you want ( could be the same). And add the routes – Papaya Labs Apr 13 '15 at 20:51
  • Whatever path I put I get an error. I want it to ideally redirect to same page like `<%= link_to content_tag(:span, '', class: 'glyphicon glyphicon-thumbs-up') + ' Like it', redirect_to(:back)(:id => comment.id), method: :post %>` but I get this error: `SyntaxError in ValuationsController#show`. When I try any other path in my routes I get this error: `Routing Error No route matches [POST] "/valuations/4"`. Thanks Manfergo for helping me this far. – AnthonyGalli.com Apr 13 '15 at 23:51
  • You can not use redirect_to in that way. You have to pass throught the comments controller method like. So after like method run you redirect _to. Can you print valuations controller and complete routes.rb to have a big picture. – Papaya Labs Apr 14 '15 at 01:33
  • I updated the question. Where would I put `redirect_to(:back)`? I tried in 3 different spots within the ruby but I got the same error. I understand why id need to use `like_comment_path` now to pass thru the comments controller. Thks! As a test I made a _like.html.erb file & like.html.erb file with `redirect_to(:back)` in it but I still got the template error. – AnthonyGalli.com Apr 14 '15 at 16:41
  • 1
    Try redirect_to(:bak) at the end of method like. If does not work, use redirect_to valuation_path(:id => @comment.commentable_id). I updated the answer. – Papaya Labs Apr 14 '15 at 17:58
  • Perfecto! Thank you I get kept putting it in the view :) – AnthonyGalli.com Apr 14 '15 at 20:18
0

Your link_to is missing what url it should post to. It should be something along these lines:

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

Notice the like_comment_path(comment) as the second argument.