2016-12-22 71 views
0

對於仍然比較新的rails新手問題,我表示歉意。我試圖向所有喜歡我的特定帖子的用戶展示。我看了一下這個類似的問題How to list the users who LIKE a given Post,但它不能解決我的問題。我在下面列出了所有相關的簡單代碼 - 非常感謝你們!如何顯示所有喜歡我的帖子的用戶

物品控制器

class ItemsController < 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 

Index.html.erb

<% if user_signed_in? %> 
    <%= image_tag current_user.avatar, width: 70, class: "css-style" %> 
    <br> 
    <strong><%= link_to current_user.username, current_user, class: "profile-style" %></strong> 
    <ul><!--Trying to show all the users who have liked my specific posts here --> 
    <% item.likes.each do |like| %> 
    <li> <%= link_to(like.user.username, like.user) %></li> 
<% end %> 
</ul> 
<br> 
<br> 
<% else %> 
    <%= link_to 'Login/SignUp', new_user_session_path %> 
<% end %> 
<% @items.each do |item| %> 
    <%= image_tag item.avatar.url(:medium), class: "block" %> 
    <div> 
     <%= render partial: "likes", locals: {item: item} %></span><%= item.likes_count %> 
    </div> 
<% end %> 

Items.rb

class Item < ApplicationRecord 
    has_many :likes, :counter_cache => true 
    belongs_to :user 
    has_attached_file :avatar, styles: { medium: "300x300>", thumb: "100x100>" }, default_url: "/images/:style/missing.png" 
    validates_attachment_content_type :avatar, content_type: /\Aimage\/.*\z/ 
end 

User.rb

class User < ApplicationRecord 
    devise :database_authenticatable, :registerable, 
     :recoverable, :rememberable, :trackable, :validatable, :omniauthable, omniauth_providers: [:facebook] 
    has_many :likes 
    has_attached_file :avatar, styles: { medium: "300x300>", thumb: "100x100>" }, 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 

用戶控制器

class UsersController < 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 

Like.rb

class Like < ApplicationRecord 
belongs_to :item, :counter_cache => true 
belongs_to :user 
end 

喜歡控制器

class Items::LikesController < 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 

日誌

Processing by ItemsController#index as HTML 

渲染布局內的項目/ index.html.erb /應用 用戶負載(0.3ms的)選擇 「用戶」。* FROM 「用戶」 WHERE 「用戶」,「。 id「=? ORDER BY「users」。「id」ASC LIMIT? [[「id」,1],[「LIMIT」,1]] 項目加載(0.3ms)選擇「items」。* FROM「items」ORDER BY created_at DESC 渲染項目/ index.html.erb在佈局/應用程序(287.6ms) 完成500內部服務器錯誤在313ms(ActiveRecord的:0.6ms)

::的ActionView ::模板錯誤(#<#未定義的局部變量或方法'項」:0x007f9afbfd14f0> 你的意思?item_url items_url item_path @items): 5:
6:<%=的link_to current_user.username,CURRENT_USER,類: 「簡檔風格」 %> 7:

    8:<%item.likes.each do | like | %> 9:
  • <%=的link_to(like.user.username,like.user)%>
  • 10:<%端%> 11:

應用/視圖/項目/ index.html中。 erb:8:在`_app_views_items_index_html_erb___2312434021832006771_70151814680620' 呈現/usr/local/lib/ruby/gems/2.3.0/gems/actionpack-5.0.0.1/lib/action_dispatch/middleware/templates/rescues/template_error.html。erb在救援/佈局 呈現/usr/local/lib/ruby/gems/2.3.0/gems/actionpack-5.0.0.1/lib/action_dispatch/middleware/templates/rescues/_source.html.erb 呈現/ 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) 渲染/ usr/local/lib/ruby​​/gems/2.3.0/gems/actionpack-5.0.0.1/lib/action_dispatch/middleware/templates/rescues/_trace.html.erb 呈現/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) 渲染/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 呈現/usr/local/lib/ruby/gems/2.3.0/gems/actionpack-5.0.0.1/lib/action_dispatch/middleware/templa tes/rescues/_request_and_response.html.erb(1.2ms) 呈現/usr/local/lib/ruby/gems/2.3.0/gems/actionpack-5.0.0.1/lib/action_dispatch/middleware/templates/rescues/template_error。 HTML.erb內救援/佈局(94.4ms)

+0

在你index.html.erb你: 「

  • <%=的link_to(l.user.username,l.user)%>
  • 」 我覺得應該是:
  • <%=的link_to(如.user.username,like.user)%>
  • 您從不在任何地方創建變量「l」,而是在您正在使用的| like |循環中。它解決了嗎? –

    +0

    @RockwellRice非常感謝您的回覆。當我這樣做時,我得到這個錯誤未定義的方法'喜歡'爲零:NilClass – Omar

    回答

    0

    這是一個簡單的錯誤。我認爲問題是你的部分..你打電話與局部locals,並在您的模板代碼中您指的是@item而不是item,這裏item不是instance變量。

    <% @items.each do |item| %> 
        <%= image_tag item.avatar.url(:medium), class: "block" %> 
        <div> 
         <%= render partial: "likes", locals: {item: item} %></span><%= item.likes_count %> 
        </div> 
    <% end %> 
    

    的事情是,當你渲染的部分與locals你需要使用當地人正常的變量不是instance變量。

    這應該工作。

    <% item.likes.each do |like| %> 
         <li> <%= link_to(like.user.username, like.user) %></li> 
    <% end %> 
    
    +0

    Minato非常感謝你的回覆。我完全遵循你的邏輯。我還將此行添加到了我的項目控制器中 - @item = Item.find(params [:id])。但是,當我刷新時出現此錯誤無法找到包含'id'= – Omar

    +0

    的項目可以發佈您的日誌嗎? – Minato

    +0

    絕對 - 通過ItemsController#index處理HTML 項目加載(0.2ms)SELECT「items」。* FROM「items」WHERE「items」。「id」=?限制? [[「id」,nil],[「LIMIT」,1]] 已完成404未在2ms內找到(ActiveRecord:0.2ms) – Omar

    0

    你需要一個表(模型)「喜歡」和協會has-many-through。事情是這樣的:

    class Item 
        has_many :likes 
        has_many :users, through: :likes 
    end 
    
    class Like 
        belongs_to :user 
        belongs_to :item 
    end 
    
    class User 
        has_many :likes 
        has_many :items 
        has_many :liked_items, through: :likes 
    end 
    
    +0

    非常感謝你的迴應。我已經有了一個類似的模型,並在我的item.rb - 我有以下(has_many:likes,:counter_cache => true)我可以像這樣修改你的代碼has_many:likers,through:likes,:counter_cache => true) – Omar

    +0

    剛剛看到你最近的編輯我得到這個錯誤,雖然「未定義的方法'喜歡'爲零:NilClass」 – Omar

    +0

    你嘗試添加'has_many:likes'到Item和User? – sig

    1

    items/index.html.erb你是第9行引用@item.likes.each,但實例變量@item尚未在items_controller#index設置。您只定義了@items,因此您收到的關於nil的錯誤未響應#likes

    如果您想查看所有項目的喜歡,有許多方法可以通過Arel實現。

    +0

    coreyward非常感謝你的迴應 - 我將此行添加到我的項目控制器@item = Item。find(params [:id]),但當我這樣做,並刷新頁面我得到這個錯誤找不到項目與'id'= – Omar

    +0

    因爲'items#index'是一個索引方法,可能沒有' ID參數(這是有道理的,因爲你沒有看到一個項目,但一個項目列表)。再次,如果你想看到所有項目的喜歡,這是你試圖檢索一個單獨的東西。 – coreyward

    +0

    在這一點上,很明顯,你進入雜草,並掀起一盤複製意大利麪。沒關係,我們都有這樣的事情發生。我最好的建議是:拿一杯新鮮的咖啡,回到Rails指南,並從頂級評論開始。 – coreyward

    相關問題