Ruby on rails ';类别';显示操作不是';t显示关联的';上市公司';(Ruby on Rails)

Ruby on rails ';类别';显示操作不是';t显示关联的';上市公司';(Ruby on Rails),ruby-on-rails,ruby,view,model-associations,Ruby On Rails,Ruby,View,Model Associations,我正在创建一个网站,用户可以发布他们的商品和服务以供出售(一个分类广告网站),并设置“列表”、“类别”和“用户”模型。列表和类别在“has_many”-->“belishing_to”关系中相互关联,类别拥有列表 然而,即使在创建新列表时列表成功地与类别关联(我相信…?),类别的“显示”页面也不会显示其关联的列表;(显示“没有要显示的列表”消息)。可能是什么问题 -这是我的分类“展示”页面: <h1><%= @category.name %></h1> <

我正在创建一个网站,用户可以发布他们的商品和服务以供出售(一个分类广告网站),并设置“列表”、“类别”和“用户”模型。列表和类别在“has_many”-->“belishing_to”关系中相互关联,类别拥有列表

然而,即使在创建新列表时列表成功地与类别关联(我相信…?),类别的“显示”页面也不会显示其关联的列表;(显示“没有要显示的列表”消息)。可能是什么问题

-这是我的分类“展示”页面:

<h1><%= @category.name %></h1>
<%= render partial: 'listings/list', locals: {
listings: @category.listings } %>
class CategoriesController < ApplicationController
  def show
    @category = Category.find(params[:id])
  end
end

-下面是类别的控制器:

<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 CategoriesController
-以下是列表的控制器:

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
class ListingsController
-这是显示页面中引用的部分内容:

<% 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
类列表{order('created_at DESC')}
验证:名称,状态:true
验证:描述,状态:true
验证:价格、状态:真实
验证:用户id,状态:true
挂载上传器:图像,图像上传器
结束
-类别的模型文件:

class Category < ActiveRecord::Base
  has_many :listings
end
类别

通过
listings
var而不是模板中的
@listings
使列表可用

只需删除@符号。


通过
listings
var而不是模板中的
@listings
使列表可用

只需删除@符号。


通过
listings
var而不是模板中的
@listings
使列表可用

只需删除@符号。


通过
listings
var而不是模板中的
@listings
使列表可用


只需删除@符号。

从类别控制器或部分列表中删除@符号?它现在显示出来了!谢谢…所以所有“列表”都可以“部分中的实例是否没有@符号?是。所有通过“局部变量”传递的实例都是局部变量。这是常见的做法。请随意将我的答案标记为“正确”:)从类别控制器或_列表部分删除@符号?它现在显示出来了!谢谢所以部分中的所有“列表”实例没有@symbol是可以的。所有通过“局部变量”传递的实例都是局部变量。这是常见的做法。请随意将我的答案标记为“正确”:)从类别控制器或_列表部分删除@符号?它现在显示出来了!谢谢所以部分中的所有“列表”实例没有@symbol是可以的。所有通过“局部变量”传递的实例都是局部变量。这是常见的做法。请随意将我的答案标记为“正确”:)从类别控制器或_列表部分删除@符号?它现在显示出来了!谢谢所以部分中的所有“列表”实例没有@symbol是可以的。所有通过“局部变量”传递的实例都是局部变量。这是常见的做法。请随意将我的答案标记为“正确”: