I created a Category model for my books model and already associated both with
我为我的书籍模型创建了一个分类模型,并且已经与之相关联
has_many :books
and
belongs_to :category
I was also able to add a simple form dropdown list in my _form.html.haml and save book entries with categories associate however; whenever I try to show all categories in my index.html.haml file I get an Action Controller: Exception caught error:
我还能够在我的_form.html.haml中添加一个简单的表单下拉列表,并保存与类别关联的书籍条目;每当我尝试在index.html.haml文件中显示所有类别时,我都会得到一个Action Controller:异常捕获错误:
"undefined local variable or method `categroy' for #<#:0x39040f8>"
“##:0x39040f8>的未定义局部变量或方法`Categroy'”
this is the code I'm using:
这是我正在使用的代码:
- Category.all.each do |category|
=link_to category.name, books_path(category: categroy.name)
EDIT
I also added an index filter in my books_controller.rb
我还在books_controller.rb中添加了一个索引过滤器
def index
if params[:category].blank?
@book = Book.all.order("created_at DESC")
else
@category_id = Category.find_by(name: params[:category]).id
@book =Book.where(category_id: @category_id).order("created_at DESC")
end
end
without the link_to code, the index filter works when I try to manually type it in the URL:
如果没有link_to代码,当我尝试在URL中手动输入索引过滤器时,索引过滤器会起作用:
http://localhost:3000/books?category=Elementary
It looks like the category model works but I'm not sure why it isn't allowing me to call category.name
看起来类别模型有效,但我不确定为什么它不允许我调用category.name
I just started rails 2 weeks ago and would really appreciate any assistance from the community. Please let me know if I need to provide more information. Thanks!
我刚刚在两周前开始使用rails,非常感谢社区提供的任何帮助。如果我需要提供更多信息,请告诉我。谢谢!
1 个解决方案
#1
you wrote categroy
, but the name of variable is category
:)
你写了类,但变量的名称是类别:)
should be =link_to category.name, books_path(category: category.name)
应该是= link_to category.name,books_path(category:category.name)
#1
you wrote categroy
, but the name of variable is category
:)
你写了类,但变量的名称是类别:)
should be =link_to category.name, books_path(category: category.name)
应该是= link_to category.name,books_path(category:category.name)