我是新來的軌道,所以我仍然試圖弄清楚所以任何幫助,非常感謝!我正在構建一個將定義「季節」的應用程序,並且會有與這些季節相關的多個「舞蹈課程」。您創建了一個賽季後,你應該有打造「danceclasses」,從而在賽季我的節目部分的選項,我有:紅寶石在軌道上關聯模型
<h2>Dance Classes Created</h2>
<%= @seasons.danceclass.each do |danceclass| %>
<p>
不過,我得到以下錯誤:
undefined method `danceclass' for nil:NilClass
我數據模型是我有一個季節表,一個舞蹈表和一個season_danceclasses表。
我對季節模式是這樣的:
class Season < ActiveRecord::Base
has_many :season_class
has_many :danceclass, through: :season_class
end
我的舞蹈類模式是這樣的:
class Danceclass < ActiveRecord::Base
belongs_to :season
has_many :student_class
has_many :student, through: :student_class
end
而且我對season_danceclass模型是這樣的:
class SeasonDanceclasses < ActiveRecord::Base
belongs_to :season
belongs_to :danceclass
end
我的season_controller看起來像這樣:
class SeasonsController < ApplicationController
before_action :set_season, only: [:show, :edit, :update, :destroy]
# GET /seasons
# GET /seasons.json
def index
@seasons = Season.all
end
# GET /seasons/1
# GET /seasons/1.json
def show
end
# GET /seasons/new
def new
@season = Season.new
end
# GET /seasons/1/edit
def edit
end
# POST /seasons
# POST /seasons.json
def create
@season = Season.new(season_params)
respond_to do |format|
if @season.save
format.html { redirect_to @season, notice: 'Season was successfully created.' }
format.json { render :show, status: :created, location: @season }
else
format.html { render :new }
format.json { render json: @season.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /seasons/1
# PATCH/PUT /seasons/1.json
def update
respond_to do |format|
if @season.update(season_params)
format.html { redirect_to @season, notice: 'Season was successfully updated.' }
format.json { render :show, status: :ok, location: @season }
else
format.html { render :edit }
format.json { render json: @season.errors, status: :unprocessable_entity }
end
end
end
# DELETE /seasons/1
# DELETE /seasons/1.json
def destroy
@season.destroy
respond_to do |format|
format.html { redirect_to seasons_url, notice: 'Season was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_season
@season = Season.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def season_params
params.require(:season).permit(:season_name, :season_start, :season_end)
end
end
我在做什麼錯?
編輯:添加Schema.rb
# encoding: UTF-8
# This file is auto-generated from the current state of the database. Instead
# of editing this file, please use the migrations feature of Active Record to
# incrementally modify your database, and then regenerate this schema definition.
#
# Note that this schema.rb definition is the authoritative source for your
# database schema. If you need to create the application database on another
# system, you should be using db:schema:load, not running all the migrations
# from scratch. The latter is a flawed and unsustainable approach (the more migrations
# you'll amass, the slower it'll run and the greater likelihood for issues).
#
# It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema.define(version: 20160729111417) do
create_table "danceclasses", force: :cascade do |t|
t.string "class_id", limit: 255
t.string "class_name", limit: 255
t.text "class_description", limit: 65535
t.integer "min_students", limit: 4
t.integer "max_students", limit: 4
t.string "category", limit: 255
t.datetime "start_date"
t.datetime "end_date"
t.integer "week_frequency", limit: 4
t.integer "day_frequency", limit: 4
t.string "start_time", limit: 255
t.string "end_time", limit: 255
t.integer "fee", limit: 4
t.string "level", limit: 255
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
create_table "season_classes", force: :cascade do |t|
t.integer "season_id", limit: 4
t.integer "danceclass_id", limit: 4
end
create_table "seasons", force: :cascade do |t|
t.string "season_name", limit: 255
t.datetime "season_start"
t.datetime "season_end"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
create_table "student_classes", force: :cascade do |t|
t.integer "student_id", limit: 4
t.integer "class_id", limit: 4
end
create_table "students", force: :cascade do |t|
t.string "first_name", limit: 255
t.string "last_name", limit: 255
t.string "student_id", limit: 255
t.datetime "date_of_birth"
t.text "notes", limit: 65535
t.string "gender", limit: 255
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
create_table "user_students", force: :cascade do |t|
t.integer "user_id", limit: 4
t.integer "student_id", limit: 4
end
create_table "users", force: :cascade do |t|
t.string "username", limit: 255
t.string "email", limit: 255
t.string "password", limit: 255
t.string "first_name", limit: 255
t.string "last_name", limit: 255
t.string "phone_number", limit: 255
t.datetime "date_of_birth"
t.string "street_1", limit: 255
t.string "street_2", limit: 255
t.string "city", limit: 255
t.string "state", limit: 255
t.string "zipcode", limit: 255
t.boolean "enabled"
t.boolean "is_admin"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
end
能否請您分享您的schema.rb?順便說一下,在聲明has_many關聯時,您應該使用複數名稱。 – utkuDAT
added schema.rb – Blogger11
它有錯誤..因爲@seasons包含超過1條記錄..所以,你只能得到1季節的許多舞蹈..它會在內部循環。 – Vishal