對於仍然比較新的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