我为新手问题道歉,这些问题对于铁路来说仍然相对较新。我试图向所有喜欢我特定帖子的用户展示。我看了一下这个类似的问题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
在布局/应用程序中渲染items / index.html.erb 用户负载(0.3ms)SELECT&#34;用户&#34;。* FROM&#34;用户&#34;用户&#34;。&#34; id&#34; =? ORDER BY&#34;用户&#34;。&#34; id&#34; ASC限制? [[&#34; id&#34;,1],[&#34; LIMIT&#34;,1]] 项目负载(0.3ms)SELECT&#34; items&#34;。* FROM&#34; items&#34; ORDER BY created_at DESC 在layouts / application中呈现items / index.html.erb(287.6ms) 在313ms完成500内部服务器错误(ActiveRecord:0.6ms)
ActionView :: Template :: Error(未定义的局部变量或方法`item&#39; for#&lt;#:0x007f9afbfd14f0&gt;
你的意思是? item_url
items_url
item_path
@items):
5:
6:&lt;%= link_to current_user.username,current_user,class:&#34; profile-style&#34; %GT;
7:
app / views / items / index.html.erb:8:在`_app_views_items_index_html_erb ___ 2312434021832006771_70151814680620&#39; 在救援/布局中渲染/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/templates/rescues/_request_and_response.html.erb(1.2ms) 在救援/布局(94.4ms)内呈现/usr/local/lib/ruby/gems/2.3.0/gems/actionpack-5.0.0.1/lib/action_dispatch/middleware/templates/rescues/template_error.html.erb
答案 0 :(得分:1)
在items/index.html.erb
中,您在第9行引用了@item.likes.each
,但实例变量@item
尚未在items_controller#index
中设置。您只定义了@items
,因此您收到的关于nil
未回复#likes
的错误。
如果你想看到所有项目的喜欢,有很多方法可以通过Arel实现这一目标。
答案 1 :(得分: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
答案 2 :(得分:0)
这是一个简单的错误。我认为问题在于您的偏见..您使用locals
调用部分内容,并在模板代码中使用@item
代替item
,{{1} }}不是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