2015-02-03 3 views
0

사용자가 상품 (게시 된 광고 사이트) & 서비스를 게시 할 수있는 웹 사이트를 만들고 '목록' , '카테고리'및 '사용자'모델을 포함합니다. 목록과 카테고리는 "has_many"-> "belongs_to"관계로 서로 연관되어 있으며 카테고리 소유 목록이 있습니다.'카테고리'표시 작업에 연결된 '목록'(Ruby on Rails)이 표시되지 않습니다.

그러나 목록이 새로운 목록 작성시 카테고리와 성공적으로 연결되어 있다고해도 (...이라고 생각하면) 카테고리의 '표시'페이지에는 관련 목록이 표시되지 않습니다. ?.

<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 

-이 부분 referenc를 나열한다 쇼 페이지에서 에드 : 목록에 대한

<% 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 

답변

3
<%= render partial: 'listings/list', locals: { 
listings: @category.listings } %> 

리스팅이 listings VAR를 통해 avalable 만든다 , 귀하의 템플릿과 마찬가지로 @listings이 아닙니다.

그냥 @ 기호를 제거하십시오.

+0

Categories 컨트롤러 또는 _list 부분에서 @ 기호를 제거 하시겠습니까? – hworth

+0

물론 부분에서 –

+0

그것은 지금 나타나고 있습니다! 고마워 ... 그래서 부분의 모든 "리스팅"인스턴스가 @ 심볼을 가지지 않는 것은 괜찮습니까? – hworth

관련 문제