NoMethodError: #<Product:0x00000000070a9810> 的未定义方法“类别”

楼主

我正在尝试为 Ruby on Rails 上的某些产品添加类别,但是我收到一条错误消息,指出NoMethodError: undefined method `categories' for #

这是我的 product.rb :

 class Product < ApplicationRecord

  validates :title, presence: true

  has_many :product_categories
  has_many :categories, through :product_categories
 end

产品类别应用记录:

class ProductCategory < ApplicationRecord

  belongs_to :product
  belongs_to :category

end

和类别.rb:

class Category < ApplicationRecord
  has_many :product_categories
  has_many :products, through :product_categories
end

在交互式 ruby​​ shell 上,我给出以下命令:

irb(main):006:0> Product.second!
Product Load (0.5ms)  SELECT  "products".* FROM "products" ORDER BY 
"products"."id" ASC LIMIT ? OFFSET ?  [["LIMIT", 1], ["OFFSET", 1]]
=> #<Product id: 2, title: "bread", description: "with glutein", price: 
0.6e0, created_at: "2019-01-13 19:42:45", updated_at: "2019-01-13 
19:42:45">
irb(main):007:0> product= _
=> #<Product id: 2, title: "bread", description: "with glutein", price: 
0.6e0, created_at: "2019-01-13 19:42:45", updated_at: "2019-01-13 19:42:45">
irb(main):008:0> product = _

=> #<Product id: 2, title: "bread", description: "with glutein", price: 
0.6e0, created_at: "2019-01-13 19:42:45", updated_at: "2019-01-13 19:42:45">
<duct.categories.create!(title: "Bread")
NoMethodError: undefined method `categories' for 

有人能帮我理解为什么会出现这个错误吗?我正在关注本教程https://www.youtube.com/watch?v=TwoafJC7vlw,它似乎在那里工作正常。

哈利勒

Product模型中请has_many :categories, through :product_categories改为has_many :categories, through: :product_categories您需要在 之后添加一个冒号throughCategory型号相同这应该可以解决错误。

此外,您还有一些其他语法错误/拼写错误,例如在产品模型validates中写为valitates.

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

未定义的方法NoMethodError

来自分类Dev

NoMethodError:未定义的方法

来自分类Dev

NoMethodError未定义的方法

来自分类Dev

未定义的方法“包括?” 对于#<String:0x2907480>(NoMethodError)

来自分类Dev

NoMethodError:#<Faq:0x00000108195e70>的未定义方法`position'

来自分类Dev

Ruby on Rails NoMethodError:#<用户:0x50987b8>的未定义方法`'

来自分类Dev

NoMethodError:#<Menu:0x512be78>的未定义方法`each'

来自分类Dev

NoMethodError:“ QLite版本3.x”的未定义方法“ fetch”:字符串

来自分类Dev

Sidekiq:NoMethodError:未定义的方法“执行”

来自分类Dev

nil:NilClass <NoMethodError>的未定义方法'>'

来自分类Dev

未定义的方法(NoMethodError)红宝石

来自分类Dev

NoMethodError:未定义的方法“ join”

来自分类Dev

NoMethodError未定义的方法“ where”

来自分类Dev

NoMethodError(未定义的方法“已锁定”

来自分类Dev

NoMethodError:未定义的方法[[] ='for nil:NilClass

来自分类Dev

NoMethodError:未定义的方法“发布”

来自分类Dev

NoMethodError:未定义的方法“创建”

来自分类Dev

nil:NilClass的未定义方法“ +”(NoMethodError)

来自分类Dev

NoMethodError:main:Object的未定义方法“ on”

来自分类Dev

NoMethodError:未定义的方法“ where”

来自分类Dev

nil:NilClass <NoMethodError>的未定义方法'[]'

来自分类Dev

rails NoMethodError:未定义的方法“酒店”

来自分类Dev

测试NoMethodError:未定义的方法

来自分类Dev

NoMethodError(未定义的方法“已锁定”

来自分类Dev

NoMethodError:未定义的方法“创建”

来自分类Dev

NoMethodError:nil:NilClass的未定义方法“ +”

来自分类Dev

NoMethodError: #<Class> 的未定义方法“类型”

来自分类Dev

ruby - NoMethodError:未定义的方法“状态”

来自分类Dev

NoMethodError:RSpec 的未定义方法“get”

Related 相关文章

  1. 1

    未定义的方法NoMethodError

  2. 2

    NoMethodError:未定义的方法

  3. 3

    NoMethodError未定义的方法

  4. 4

    未定义的方法“包括?” 对于#<String:0x2907480>(NoMethodError)

  5. 5

    NoMethodError:#<Faq:0x00000108195e70>的未定义方法`position'

  6. 6

    Ruby on Rails NoMethodError:#<用户:0x50987b8>的未定义方法`'

  7. 7

    NoMethodError:#<Menu:0x512be78>的未定义方法`each'

  8. 8

    NoMethodError:“ QLite版本3.x”的未定义方法“ fetch”:字符串

  9. 9

    Sidekiq:NoMethodError:未定义的方法“执行”

  10. 10

    nil:NilClass <NoMethodError>的未定义方法'>'

  11. 11

    未定义的方法(NoMethodError)红宝石

  12. 12

    NoMethodError:未定义的方法“ join”

  13. 13

    NoMethodError未定义的方法“ where”

  14. 14

    NoMethodError(未定义的方法“已锁定”

  15. 15

    NoMethodError:未定义的方法[[] ='for nil:NilClass

  16. 16

    NoMethodError:未定义的方法“发布”

  17. 17

    NoMethodError:未定义的方法“创建”

  18. 18

    nil:NilClass的未定义方法“ +”(NoMethodError)

  19. 19

    NoMethodError:main:Object的未定义方法“ on”

  20. 20

    NoMethodError:未定义的方法“ where”

  21. 21

    nil:NilClass <NoMethodError>的未定义方法'[]'

  22. 22

    rails NoMethodError:未定义的方法“酒店”

  23. 23

    测试NoMethodError:未定义的方法

  24. 24

    NoMethodError(未定义的方法“已锁定”

  25. 25

    NoMethodError:未定义的方法“创建”

  26. 26

    NoMethodError:nil:NilClass的未定义方法“ +”

  27. 27

    NoMethodError: #<Class> 的未定义方法“类型”

  28. 28

    ruby - NoMethodError:未定义的方法“状态”

  29. 29

    NoMethodError:RSpec 的未定义方法“get”

热门标签

归档