2016-12-22 76 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

    相关问题