我正在尝试使用lynda.com学习Rails在这里找到教程:
https://www.lynda.com/Ruby-Rails-tutorials/Ruby-Rails-5-Essential-Training/500551-2.html
到目前为止,它很容易被追踪,但我遇到了障碍。在了解协会时,我应该看到这一点(从课程视频中捕获):
相反,我收到以下错误:
NoMethodError:#< Subject:0x
的未定义方法`page'
我使用教师使用的相同page.rb和subject.rb:
# page.rb file
class Page < ApplicationRecord
belongs_to :subject, { :optional => false }
has_many :sections
has_and_belongs_to_many :admin_users
end
# subject.rb file
class Subject < ApplicationRecord
has_many :pages
scope :visible, lambda { where(:visible => true) }
scope :invisible, lambda { where(:visible => false) }
scope :sorted, lambda { order("position ASC") }
scope :newest_first, lambda { order("created_at DESC") }
scope :search, lambda {|query| where(["name LIKE ?", "%#{query}%"]) }
end
mysql数据库看起来像这样:
mysql> SHOW TABLES;
+----------------------------------+
| Tables_in_simple_cms_development |
+----------------------------------+
| admin_users_pages |
| ar_internal_metadata |
| pages |
| schema_migrations |
| section_edits |
| sections |
| subjects |
| users |
+----------------------------------+
mysql> DESCRIBE pages;
+------------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+------------+--------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| subject_id | int(11) | YES | MUL | NULL | |
| name | varchar(255) | YES | | NULL | |
| permalink | varchar(255) | YES | MUL | NULL | |
| position | int(11) | YES | | NULL | |
| visible | tinyint(1) | YES | | 0 | |
| created_at | datetime | NO | | NULL | |
| updated_at | datetime | NO | | NULL | |
+------------+--------------+------+-----+---------+----------------+
mysql> DESCRIBE subjects;
+------------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+------------+--------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| name | varchar(255) | YES | | NULL | |
| position | int(11) | YES | | NULL | |
| visible | tinyint(1) | YES | | 0 | |
| created_at | datetime | NO | | NULL | |
| updated_at | datetime | NO | | NULL | |
+------------+--------------+------+-----+---------+----------------+
我查看了很多与NoMethodError相关的stackoverflow问题,但是大多数问题都是关于'require'的,我们还没有讨论过。我正在运行与讲师相同的rails开发环境(Rails 5.0.0)。他在Mac上运行,我有一台Windows 10机器,如果这有所不同。
我不是要求任何人“为我解决这个问题”,但我不知道接下来要去哪儿。
答案 0 :(得分:1)
您在主题和页面之间使用has_many关联,这意味着您无法使用
subject.page
而你应该使用
subject.pages