Ruby on rails 关联:找不到主题项

Ruby on rails 关联:找不到主题项,ruby-on-rails,Ruby On Rails,尝试创建新学生时,收到错误:找不到关联:主题\u项 出现错误的文件是app/views/students/\u form.haml: = simple_form_for student do |f| = f.input :first_name = f.input :last_name = f.association :subject_items, label_method: :title, value_method: :id, label: 'Subject Items', as:

尝试创建新学生时,收到错误:找不到关联
:主题\u项

出现错误的文件是
app/views/students/\u form.haml

= simple_form_for student do |f|
  = f.input :first_name
  = f.input :last_name
  = f.association :subject_items, label_method: :title, value_method: :id, label: 'Subject Items', as: :check_boxes
  = link_to t('shared.back'), students_path, class: 'btn btn-default'
  = f.button :submit
型号:

class Student < ActiveRecord::Base
  has_many :participations, dependent: :destroy
  has_many :subject_item_notes, dependent: :destroy
  validates :first_name, :last_name, presence: true
end

我在这里遗漏了什么?

您遇到的问题是,当关联不存在时,您正在调用
f.association:subject\u items

它应该类似于这样:

#app/models/student.rb
class Student < ActiveRecord::Base
   has_many :subject_items
end
这将允许您使用
f.association
输入,如下所示:

 = simple_form_for student do |f|
  = f.input :first_name
  = f.input :last_name
  = f.association :subjects, label_method: :title, value_method: :id, label: 'Subject Items', as: :check_boxes
  = link_to t('shared.back'), students_path, class: 'btn btn-default'
  = f.button :submit

笔记 作为第二个注意事项,您的示意图中还有一些其他内容需要更新

1。路线

#config/routes.rb
root to:  'students#index'

resources :visitors, :subject_items
resources :students, :teachers, :reports do
  resources :subjects, only: :index
end
使用in-Rails管线可以极大地提高管线定义的效率

我刚刚测试过,您也可以向这些路由添加嵌套资源

--

2。型号

#config/routes.rb
root to:  'students#index'

resources :visitors, :subject_items
resources :students, :teachers, :reports do
  resources :subjects, only: :index
end
在我上面的推荐中,我使用了
has\u和\u-belient\u-to\u-many
关联

如果您的联接模型中没有任何关联数据,则这优先于
的has-many:to
,尽管它们会获得类似的结果

您可能有这样的数据——“主题项目”——在这种情况下,建议您使用
has\u many:through
关系

如果您需要,我可以更新答案;Rails文档解释得很好

简言之,我想说的是,您应该将您的
注释保存在加入模型(“参与”)中:

#app/models/student.rb
班级学生相当于“课堂”
有很多:主题,通过::参与
结束
#app/models/participation.rb
课堂参与
您遇到的问题是,当关联不存在时,您正在调用
f.association:subject\u items

它应该类似于这样:

#app/models/student.rb
class Student < ActiveRecord::Base
   has_many :subject_items
end
这将允许您使用
f.association
输入,如下所示:

 = simple_form_for student do |f|
  = f.input :first_name
  = f.input :last_name
  = f.association :subjects, label_method: :title, value_method: :id, label: 'Subject Items', as: :check_boxes
  = link_to t('shared.back'), students_path, class: 'btn btn-default'
  = f.button :submit

笔记 作为第二个注意事项,您的示意图中还有一些其他内容需要更新

1。路线

#config/routes.rb
root to:  'students#index'

resources :visitors, :subject_items
resources :students, :teachers, :reports do
  resources :subjects, only: :index
end
使用in-Rails管线可以极大地提高管线定义的效率

我刚刚测试过,您也可以向这些路由添加嵌套资源

--

2。型号

#config/routes.rb
root to:  'students#index'

resources :visitors, :subject_items
resources :students, :teachers, :reports do
  resources :subjects, only: :index
end
在我上面的推荐中,我使用了
has\u和\u-belient\u-to\u-many
关联

如果您的联接模型中没有任何关联数据,则这优先于
的has-many:to
,尽管它们会获得类似的结果

您可能有这样的数据——“主题项目”——在这种情况下,建议您使用
has\u many:through
关系

如果您需要,我可以更新答案;Rails文档解释得很好

简言之,我想说的是,您应该将您的
注释保存在加入模型(“参与”)中:

#app/models/student.rb
班级学生相当于“课堂”
有很多:主题,通过::参与
结束
#app/models/participation.rb
课堂参与
首先,您的
f.association
引用了模型上不存在的关联(
:subject\u items
,其中您的关联是
:subject\u item\u notes
)。这可能是一个奇怪的错误消息。首先,您的
f.association
引用了模型上不存在的关联(
:subject\u items
,其中您的关联是
:subject\u item\u notes
)。这可能是一个奇怪的错误信息。