我的代码是这样的:
路线.rb
resources :challenges do
resources :problems
end
应用程序/模型/挑战.rb
class Challenge < ActiveRecord::Base
has_many :problems
end
应用程序/模型/问题.rb
class Problem < ActiveRecord::Base
belongs_to :challenge
end
db/migrate/createchallenges.db
class CreateChallenges < ActiveRecord::Migration
def change
create_table :challenges do |t|
t.string :name
t.timestamps
end
end
end
db/migrate/createproblems.rb
class CreateProblems < ActiveRecord::Migration
def change
create_table :problems do |t|
t.belongs_to :challenge
t.string :description
t.timestamps
end
end
结尾
应用程序/控制器/problem_controller.rb
class ProblemsController < ApplicationController
def new
@challenge = Challenge.find(params[:challenge_id])
@problem = Problem.new(:challenge => @challenge)
end
end
我可以毫无问题地创建一个“挑战”,但是当我创建一个新的“问题”时,我得到了错误:
can't write unknown attribute `challenge_id'
在线上
@problem = Problem.new(:challenge => @challenge)
什么可能导致此错误?