title: How to show all the Users who have liked my post
body: <p>I apologize for the newbie questions still relatively new to rails. I'm trying to show all the users who have liked my specific posts. I took a look at this similar question <a href="https://stackoverflow.com/questions/16435778/how-to-list-the-users-who-like-a-given-post]">How to list the users who LIKE a given Post</a> but it couldn't solve my problem. I listed below all the relevant simple code - thank you so much guys!!</p>
  
  <p><strong>Items Controller</strong></p>
  
  <pre><code>class ItemsController &lt; ApplicationController
      before_action :authenticate_user!, only: [:new, :create]
      before_action :set_item, only: [:show, :edit, :update, :destroy, :share]
  
      def index    
          @items = Item.order("created_at DESC")
      end
  end
  </code></pre>
  
  <p><strong>Index.html.erb</strong></p>
  
  <pre><code>&lt;% if user_signed_in? %&gt;
      &lt;%= image_tag current_user.avatar, width: 70, class: "css-style"  %&gt;
      &lt;br&gt;
      &lt;strong&gt;&lt;%= link_to current_user.username, current_user, class: "profile-style" %&gt;&lt;/strong&gt;
      &lt;ul&gt;&lt;!--Trying to show all the users who have liked my specific posts here --&gt;
      &lt;% item.likes.each do |like| %&gt;
    &lt;li&gt; &lt;%= link_to(like.user.username, like.user) %&gt;&lt;/li&gt;
  &lt;% end %&gt;
  &lt;/ul&gt;
  &lt;br&gt;
  &lt;br&gt;
  &lt;% else %&gt;
      &lt;%= link_to 'Login/SignUp', new_user_session_path %&gt;
  &lt;% end %&gt;
  &lt;% @items.each do |item| %&gt;
      &lt;%= image_tag item.avatar.url(:medium), class: "block" %&gt;
      &lt;div&gt;
          &lt;%= render partial: "likes", locals: {item: item} %&gt;&lt;/span&gt;&lt;%= item.likes_count %&gt;
      &lt;/div&gt;
  &lt;% end %&gt;
  </code></pre>
  
  <p><strong>Items.rb</strong></p>
  
  <pre><code>class Item &lt; ApplicationRecord
      has_many :likes, :counter_cache =&gt; true
      belongs_to :user
      has_attached_file :avatar, styles: { medium: "300x300&gt;", thumb: "100x100&gt;" }, default_url: "/images/:style/missing.png"
      validates_attachment_content_type :avatar, content_type: /\Aimage\/.*\z/
  end
  </code></pre>
  
  <p><strong>User.rb</strong></p>
  
  <pre><code>class User &lt; ApplicationRecord
      devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable, :omniauthable, omniauth_providers: [:facebook]
      has_many :likes
      has_attached_file :avatar, styles: { medium: "300x300&gt;", thumb: "100x100&gt;" }, default_url: "/images/:style/missing.png"
      validates_attachment_content_type :avatar, content_type: /\Aimage\/.*\z/
  
      def likes?(post)
          post.likes.where(user_id: id).any?
      end
  end
  </code></pre>
  
  <p><strong>Users Controller</strong></p>
  
  <pre><code>class UsersController &lt; ApplicationController
      before_action :set_user, only: [:show, :edit, :update, :destroy, :share]
      def index
          @users = User.all
      end
  
      def show
          @user = User.find(params[:id])
          @items = Item.all
      end
  
      private
  
      def set_user
          @user = User.find(params[:id])
      end
  
      def item_params
          params.require(:item).permit(:product, :amount, :city_id, :avatar)
      end
  end
  </code></pre>
  
  <p><strong>Like.rb</strong></p>
  
  <pre><code>class Like &lt; ApplicationRecord
  belongs_to :item, :counter_cache =&gt; true
  belongs_to :user
  end
  </code></pre>
  
  <p><strong>Likes Controller</strong></p>
  
  <pre><code>class Items::LikesController &lt; ApplicationController
  before_action :authenticate_user!
  before_action :set_book
  
  def create
  
      @item.likes.where(user_id: current_user.id).first_or_create
  
  
      respond_to do |format|
          format.html {redirect_to @item}
          format.js
      end
  end
  
  
  def destroy
      @item.likes.where(user_id: current_user.id).destroy_all
  
      respond_to do |format|
          format.html {redirect_to @item}
          format.js
      end
  end
  private
  def set_book
      @item = Item.find(params[:item_id])
  end
  end
  </code></pre>
  
  <p><strong>Logs</strong></p>
  
  <pre><code>Processing by ItemsController#index as HTML
  </code></pre>
  
  <p>Rendering items/index.html.erb within layouts/application
    User Load (0.3ms)  SELECT  "users".* FROM "users" WHERE "users"."id" = ? ORDER BY "users"."id" ASC LIMIT ?  [["id", 1], ["LIMIT", 1]]
    Item Load (0.3ms)  SELECT "items".* FROM "items" ORDER BY created_at DESC
    Rendered items/index.html.erb within layouts/application (287.6ms)
  Completed 500 Internal Server Error in 313ms (ActiveRecord: 0.6ms)</p>
  
  <p>ActionView::Template::Error (undefined local variable or method `item' for #&lt;#:0x007f9afbfd14f0>
  Did you mean?  item_url
                 items_url
                 item_path
                 @items):
       5: <br>
       6: <strong>&lt;%= link_to current_user.username, current_user, class: "profile-style" %></strong>
       7: <ul>
       8:         &lt;% item.likes.each do |like| %>
       9:       <li> &lt;%= link_to(like.user.username, like.user) %></li>
      10: &lt;% end %>
      11:     </ul></p>
  
  <p>app/views/items/index.html.erb:8:in `_app_views_items_index_html_erb___2312434021832006771_70151814680620'
    Rendering /usr/local/lib/ruby/gems/2.3.0/gems/actionpack-5.0.0.1/lib/action_dispatch/middleware/templates/rescues/template_error.html.erb within rescues/layout
    Rendering /usr/local/lib/ruby/gems/2.3.0/gems/actionpack-5.0.0.1/lib/action_dispatch/middleware/templates/rescues/_source.html.erb
    Rendered /usr/local/lib/ruby/gems/2.3.0/gems/actionpack-5.0.0.1/lib/action_dispatch/middleware/templates/rescues/_source.html.erb (8.3ms)
    Rendering /usr/local/lib/ruby/gems/2.3.0/gems/actionpack-5.0.0.1/lib/action_dispatch/middleware/templates/rescues/_trace.html.erb
    Rendered /usr/local/lib/ruby/gems/2.3.0/gems/actionpack-5.0.0.1/lib/action_dispatch/middleware/templates/rescues/_trace.html.erb (3.3ms)
    Rendering /usr/local/lib/ruby/gems/2.3.0/gems/actionpack-5.0.0.1/lib/action_dispatch/middleware/templates/rescues/_request_and_response.html.erb
    Rendered /usr/local/lib/ruby/gems/2.3.0/gems/actionpack-5.0.0.1/lib/action_dispatch/middleware/templates/rescues/_request_and_response.html.erb (1.2ms)
    Rendered /usr/local/lib/ruby/gems/2.3.0/gems/actionpack-5.0.0.1/lib/action_dispatch/middleware/templates/rescues/template_error.html.erb within rescues/layout (94.4ms)</p>