0
嗨我在使用simple_form創建關聯時遇到了問題。型號章屬於主題:用於simple_form的滑軌窗體關聯
class Subject < ActiveRecord::Base
validates :name, :presence => true,
:length => {:maximum => 30},
:uniqueness => true
has_many :chapters
end
模型章:
class Chapter < ActiveRecord::Base
validates :name, :presence => true,
:length => {:maximum => 80}
validates :subject_id, :presence => true
belongs_to :subject
end
控制器章
def new
@chapter = Chapter.new
end
def create
@chapter = Chapter.new(chapter_params)
if @chapter.save
flash[:notice] = "Chapter created successfully."
redirect_to(:action => 'index')
else
render('new')
end
end
private
def chapter_params
params.require(:chapter).permit(:name, :permalink, :subject_id,
:introduction, :free, :active, :position, :semester)
end
表格新章
我得到以下錯誤:
「關聯不能用於與對象無關的表單中。」
有什麼我需要添加到我的控制器或模型?當我不使用關聯和簡單地輸入主題的ID時,一切正常。
儘管向控制器添加構建方法是有道理的,但仍然無法正常工作。 – Hacktacus
將simple_form_for調用從:chapter更改爲simple_form_for @chapter do | f | – nunopolonia