对于仍然比较新的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)
在你index.html.erb你: “
@RockwellRice非常感谢您的回复。当我这样做时,我得到这个错误未定义的方法'喜欢'为零:NilClass – Omar