'Categories' 显示操作未显示关联的 'Listings'(Ruby 在 Rails 上)
'Categories' show action isn't displaying associated 'Listings' (Ruby on Rails)
我正在创建一个网站,用户可以在其中 post 出售他们的商品和服务(分类广告网站)并设置了 'listing'、'category' 和 'user' 模型。列表和类别以 "has_many"-->"belongs_to" 关系相互关联,类别拥有列表。
但是,即使在创建新列表时列表已成功与类别关联(我相信......?),"show" 页面类别未显示其关联列表; (显示 "No listings to display" 消息”)。可能是什么问题?
-这是我的 'show' 类别页面:
<h1><%= @category.name %></h1>
<%= render partial: 'listings/list', locals: {
listings: @category.listings } %>
-这是类别的控制器:
class CategoriesController < ApplicationController
def show
@category = Category.find(params[:id])
end
end
-这是列表的控制器:
class ListingsController < ApplicationController
before_action :logged_in_user, only: [:create, :destroy]
before_action :correct_user, except: [:create, :index, :new]
def index
@listings = Listing.all
end
def show
end
def new
@listing = Listing.new
end
def edit
end
def create
@listing = current_user.listings.build(listing_params)
if @listing.save
redirect_to @listing
flash[:success] = "Listing was successfully created."
else
render 'new'
end
end
def update
if @listing.update(listing_params)
flash[:success] = "Listing was successfully updated."
redirect_to @listing
else
render 'edit'
end
end
def destroy
@listing.destroy
flash[:success] = "Listing deleted."
redirect_to request.referrer || root_url
end
private
def listing_params
params.require(:listing).permit(:name, :description, :price, :image,
:category_id)
def correct_user
@listing = current_user.listings.find_by(id: params[:id])
redirect_to root_url if @listing.nil?
end
end
-这是展示页面中部分引用的列表:
<% if @listings.nil? %>
No listings to display! Go <%= link_to 'create one', new_listing_path %>.
<% else %>
<table class="table table-striped">
<tbody>
<% @listings.each do |listing| %>
<tr>
<td><%= link_to listing.name, listing %></td>
<td class="text-right">
<% if listing.price %>
<%= number_to_currency(listing.price) %>
<% end %>
</td>
<td class="text-right">
<% @listings.each do |listing| %>
<% if listing.category %>
<%= link_to listing.category.name, listing.category %>
<% end %>
<% end %>
</td>
</tr>
<% end %>
</tbody>
</table>
<% end %>
-上市模型文件:
class Listing < ActiveRecord::Base
belongs_to :user
belongs_to :category
default_scope -> { order('created_at DESC') }
validates :name, presence: true
validates :description, presence: true
validates :price, presence: true
validates :user_id, presence: true
mount_uploader :image, ImageUploader
end
-类别的模型文件:
class Category < ActiveRecord::Base
has_many :listings
end
<%= render partial: 'listings/list', locals: {
listings: @category.listings } %>
通过 listings
var 使列表可用,而不是像在您的模板中那样 @listings
。
只需删除@符号。
我正在创建一个网站,用户可以在其中 post 出售他们的商品和服务(分类广告网站)并设置了 'listing'、'category' 和 'user' 模型。列表和类别以 "has_many"-->"belongs_to" 关系相互关联,类别拥有列表。
但是,即使在创建新列表时列表已成功与类别关联(我相信......?),"show" 页面类别未显示其关联列表; (显示 "No listings to display" 消息”)。可能是什么问题?
-这是我的 'show' 类别页面:
<h1><%= @category.name %></h1>
<%= render partial: 'listings/list', locals: {
listings: @category.listings } %>
-这是类别的控制器:
class CategoriesController < ApplicationController
def show
@category = Category.find(params[:id])
end
end
-这是列表的控制器:
class ListingsController < ApplicationController
before_action :logged_in_user, only: [:create, :destroy]
before_action :correct_user, except: [:create, :index, :new]
def index
@listings = Listing.all
end
def show
end
def new
@listing = Listing.new
end
def edit
end
def create
@listing = current_user.listings.build(listing_params)
if @listing.save
redirect_to @listing
flash[:success] = "Listing was successfully created."
else
render 'new'
end
end
def update
if @listing.update(listing_params)
flash[:success] = "Listing was successfully updated."
redirect_to @listing
else
render 'edit'
end
end
def destroy
@listing.destroy
flash[:success] = "Listing deleted."
redirect_to request.referrer || root_url
end
private
def listing_params
params.require(:listing).permit(:name, :description, :price, :image,
:category_id)
def correct_user
@listing = current_user.listings.find_by(id: params[:id])
redirect_to root_url if @listing.nil?
end
end
-这是展示页面中部分引用的列表:
<% if @listings.nil? %>
No listings to display! Go <%= link_to 'create one', new_listing_path %>.
<% else %>
<table class="table table-striped">
<tbody>
<% @listings.each do |listing| %>
<tr>
<td><%= link_to listing.name, listing %></td>
<td class="text-right">
<% if listing.price %>
<%= number_to_currency(listing.price) %>
<% end %>
</td>
<td class="text-right">
<% @listings.each do |listing| %>
<% if listing.category %>
<%= link_to listing.category.name, listing.category %>
<% end %>
<% end %>
</td>
</tr>
<% end %>
</tbody>
</table>
<% end %>
-上市模型文件:
class Listing < ActiveRecord::Base
belongs_to :user
belongs_to :category
default_scope -> { order('created_at DESC') }
validates :name, presence: true
validates :description, presence: true
validates :price, presence: true
validates :user_id, presence: true
mount_uploader :image, ImageUploader
end
-类别的模型文件:
class Category < ActiveRecord::Base
has_many :listings
end
<%= render partial: 'listings/list', locals: {
listings: @category.listings } %>
通过 listings
var 使列表可用,而不是像在您的模板中那样 @listings
。
只需删除@符号。