0

我是Ruby on Rails的新手,我從一個腳手架開始並手動添加了另一個模型。我似乎無法從手動生成的模型中獲取值以顯示在我的索引視圖中。無法在我的導軌模型中顯示值

我的第一個模型是高爾夫球場名稱,城市,標準桿和hole_id。第二個模型是每個課程的孔數量。出於某種原因,我無法獲得顯示的洞數下面是我的代碼。 模型

class Course < ActiveRecord::Base 
    has_many :holes 
end 

class Hole < ActiveRecord::Base 
    belongs_to :course 
end 

控制器

class CoursesController < ApplicationController 
    before_action :set_course, only: [:show, :edit, :update, :destroy] 

    # GET /courses 
    # GET /courses.json 
    def index 
    @courses = Course.all 
    @holes = Hole.all 
    end 

    # GET /courses/1 
    # GET /courses/1.json 
    def show 
    end 

    # GET /courses/new 
    def new 
    @course = Course.new 

    end 

    # GET /courses/1/edit 
    def edit 
    end 

    # POST /courses 
    # POST /courses.json 
    def create 
    @course = Course.new(course_params) 

    respond_to do |format| 
     if @course.save 
     format.html { redirect_to @course, notice: 'Course was successfully created.' } 
     format.json { render :show, status: :created, location: @course } 
     else 
     format.html { render :new } 
     format.json { render json: @course.errors, status: :unprocessable_entity } 
     end 
    end 
    end 

    # PATCH/PUT /courses/1 
    # PATCH/PUT /courses/1.json 
    def update 
    respond_to do |format| 
     if @course.update(course_params) 
     format.html { redirect_to @course, notice: 'Course was successfully updated.' } 
     format.json { render :show, status: :ok, location: @course } 
     else 
     format.html { render :edit } 
     format.json { render json: @course.errors, status: :unprocessable_entity } 
     end 
    end 
    end 

    # DELETE /courses/1 
    # DELETE /courses/1.json 
    def destroy 
    @course.destroy 
    respond_to do |format| 
     format.html { redirect_to courses_url, notice: 'Course was successfully destroyed.' } 
     format.json { head :no_content } 
    end 
    end 

    private 
    # Use callbacks to share common setup or constraints between actions. 
    def set_course 
     @course = Course.find(params[:id]) 
    end 

    # Never trust parameters from the scary internet, only allow the white list through. 
    def course_params 
     params.require(:course).permit(:name, :city, :hole_id) 
    end 
end 

查看

<p id="notice"><%= notice %></p> 

<p> 
    <strong>Name:</strong> 
    <%= @course.name %> 
</p> 

<p> 
    <strong>City:</strong> 
    <%= @course.city %> 
</p> 

<p> 
    <strong>Hole:</strong> 
    <%= @course.holes %> 
</p> 

<%= link_to 'Edit', edit_course_path(@course) %> | 
<%= link_to 'Back', courses_path %> 
+0

它看起來好像你在你的「索引」控制器方法上調用@courses,但試圖在「顯示」視圖中找到漏洞。 – Leonardo

回答