Add a Like / Unlike button to submit

I have applied a simple button type / as opposed to my application using act_as_votable. I am new to ROR so I might be missing something very simple.

I would like the user to be able to click on like or as opposed to a link and it updates the score. Any help would be greatly appreciated. Thank you in advance.

Migration

class ActsAsVotableMigration < ActiveRecord::Migration
  def self.up
    create_table :votes do |t|

      t.references :votable, :polymorphic => true
      t.references :voter, :polymorphic => true

      t.boolean :vote_flag
      t.string :vote_scope
      t.integer :vote_weight

      t.timestamps
    end

    if ActiveRecord::VERSION::MAJOR < 4
      add_index :votes, [:votable_id, :votable_type]
      add_index :votes, [:voter_id, :voter_type]
    end

    add_index :votes, [:voter_id, :voter_type, :vote_scope]
    add_index :votes, [:votable_id, :votable_type, :vote_scope]
  end

  def self.down
    drop_table :votes
  end
end

      

Mail Controller

class PostsController < ApplicationController
  before_action :set_post, only: [:show, :edit, :update, :destroy]
  before_action :correct_user, only: [:edit, :update, :destroy]
  before_action :authenticate_user!, except: [:index, :show]


  def index
    @posts = Post.all.order("created_at DESC").paginate(:page => params[:page], :per_page => 40)
  end


  def show
  end


  def new
    @post = current_user.posts.build
  end


  def edit
  end


  def create
    @post = current_user.posts.build(post_params)

    respond_to do |format|
      if @post.save
        format.html { redirect_to @post, notice: 'Post was successfully created.' }
        format.json { render action: 'show', status: :created, location: @post }
      else
        format.html { render action: 'new' }
        format.json { render json: @post.errors, status: :unprocessable_entity }
      end
    end
  end


  def update
    respond_to do |format|
      if @post.update(post_params)
        format.html { redirect_to @post, notice: 'Post was successfully updated.' }
        format.json { head :no_content }
      else
        format.html { render action: 'edit' }
        format.json { render json: @post.errors, status: :unprocessable_entity }
      end
    end
  end


  def destroy
    @post.destroy
    respond_to do |format|
      format.html { redirect_to posts_url }
      format.json { head :no_content }
    end
  end


  def upvote
      @post = Post.find(params[:id])
      @post.liked_by current_user
      redirect_to @post
  end

  def downvote
      @post = Post.find(params[:id])
      @post.downvote_from current_user
      redirect_to @post
  end


  private
    # Use callbacks to share common setup or constraints between actions.
    def set_post
      @post = Post.find(params[:id])
    end

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

    # Never trust parameters from the scary internet, only allow the white list through.
    def post_params
      params.require(:post).permit(:description, :image)
    end

  end

      

Post / Show

<div class="row">
    <div class="col-md-offset-4 col-med-8">
        <div class="panel panel-default">
        <div class="panel-heading center">

        <% if @post.image.url %>
            <%= image_tag @post.image.url(:medium) %>

        <% elsif @post.video.url %>
            <%= video_tag @post.video.url(:medium), controls: true, type: "video/mp4" %>
        <% end %></br>
        <%= link_to "Like", like_post_path(@post), method: :put %>
        <%= link_to "Dislike", dislike_post_path(@post), method: :put %>
        </div>
        <div class="panel-body">
        <center><p><%= @post.description %></p></center>

        <% user = @post.user %>
        <center><p><strong><%= link_to(user.name, user_path(user)) if @post.user %></strong></p></center>

        <% if @post.user == current_user %>
            <%= link_to edit_post_path(@post) do %>
            <span class="glyphicon glyphicon-edit"></span>
            Edit
          <% end %>
        <% end %>
        <center><%= link_to 'Back', posts_path %></center>
        </div>
</div>

      

Routes

Uscout::Application.routes.draw do

  devise_for :admins
  devise_for :users

  resources :posts

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

  resources :relationships, only: [:create, :destroy]
  resources :user_friendships do 
      member do
        put :accept
      end
     end   

  resources :post do
  member do
    put "like", to: "posts#upvote"
    put "dislike", to: "posts#downvote"
  end
end

      

Publishing model

class Post < ActiveRecord::Base

    belongs_to :user

    has_attached_file :image, :styles => { :medium => "300x300>", :thumb => "100x100>" }

    validates_attachment_content_type :image, :content_type => ["image/jpg", "image/jpeg", "image/png"]

    validates :description, presence: true
    validates :image, presence: true

  scope :subscribed, ->(following) { where user_id: following }

  acts_as_votable

    # Returns posts from the users being followed by the given user.
  def self.from_users_followed_by(user)
    followed_user_ids = user.followed_user_ids
    where("user_id IN (:followed_user_ids) OR user_id = :user_id",
          followed_user_ids: followed_user_ids, user_id: user)
  end

end

      

+3


source to share


1 answer


You have two typos.

The status bar uses a local variable like

and also uses a single equal sign:

if like = 

      



You need to use an instance variable @like

coming from your controller and also use two equal signs:

if @like == 

      

+2


source







All Articles