在 http://localhost:3000/entries/view_all 上引发错误
Raise an error on http://localhost:3000/entries/view_all
大家好,我目前正在做第 13 章的开头 ruby。我在访问 http://localhost:3000/entries/view_all 时遇到了这个错误。即使我试图查看我的数据库条目的值,它仍然根本不起作用。
这是预期的结果
这是我现在面临的错误:
Couldn't find all Entries with 'id': (all, {:include=>:Content, :order=>"created_at DESC"}) (found 0 results, but was looking for 2)
代码:
def view_all
#@entries = Entry.all(:order => 'created_at DESC')
@entries = Entry.find(:all, :include => :Content, :order => "created_at DESC")
end
这是我的条目控制器:
def view_all
@entries = Entry.find(:all, :include => :Content, :order => "created_at DESC")
end
这是我的 routes.rb:
Rails.application.routes.draw do
resources :users
resources :entries do
get :view_all, on: :collection
end
end
这是我的 view_all.html.erb:
<% @entries.each do |entry| %>
<h1><%= entry.Title %></h1>
<p><%= entry.Content %></p>
<p><em>Posted at <%= entry.created_at %></em></p>
<% end %>
<%= link_to 'Add New Entry', new_entry_path %>
从 Rails 4 开始不推荐使用查找语法。
@entries = Entry.find(:all, :include => :Content, :order => "created_at DESC")
应该成为。
@entries = Entry.where(:include => :Content, :order => "created_at DESC")
您收到此错误是因为 find
方法试图根据 ID 查找所有记录 (已解释 here)。所以字符串 :all, :include => :Content, :order => "created_at DESC"
被视为 ID,这就是 rails 抱怨的原因。
相反,要获得 all 条记录(没有约束),请使用恰当命名的 all method。你的代码应该是这样的:
@entries = Entry.includes(:content).order('created_at DESC').all
更新
显然,Content
是 Entry
模型上的一个属性的名称,因此 this 实际上应该是这样的:
@entries = Entry.order('created_at DESC').all
为了将来参考,您真的应该按照 ruby 代码的习惯用小写字母命名您的属性。 IE。
class Entry
attr_accessor :content, :title
end
相对于
class Entry
attr_accessor :Content, :Title
end
祝你升学顺利:-)
如果您使用的 rails 版本大于 4,那么 find
方法只需要 id
或多个 ids
作为参数以便获取所有行,您可以这样做:
def view_all
@entries = Entry.all.includes(:Content).order(created_at: :desc)
end
或者对于旧版本,您可以按照问题评论中的建议进行操作
大家好,我目前正在做第 13 章的开头 ruby。我在访问 http://localhost:3000/entries/view_all 时遇到了这个错误。即使我试图查看我的数据库条目的值,它仍然根本不起作用。
这是预期的结果
这是我现在面临的错误:
Couldn't find all Entries with 'id': (all, {:include=>:Content, :order=>"created_at DESC"}) (found 0 results, but was looking for 2)
代码:
def view_all
#@entries = Entry.all(:order => 'created_at DESC')
@entries = Entry.find(:all, :include => :Content, :order => "created_at DESC")
end
这是我的条目控制器:
def view_all
@entries = Entry.find(:all, :include => :Content, :order => "created_at DESC")
end
这是我的 routes.rb:
Rails.application.routes.draw do
resources :users
resources :entries do
get :view_all, on: :collection
end
end
这是我的 view_all.html.erb:
<% @entries.each do |entry| %>
<h1><%= entry.Title %></h1>
<p><%= entry.Content %></p>
<p><em>Posted at <%= entry.created_at %></em></p>
<% end %>
<%= link_to 'Add New Entry', new_entry_path %>
从 Rails 4 开始不推荐使用查找语法。
@entries = Entry.find(:all, :include => :Content, :order => "created_at DESC")
应该成为。
@entries = Entry.where(:include => :Content, :order => "created_at DESC")
您收到此错误是因为 find
方法试图根据 ID 查找所有记录 (已解释 here)。所以字符串 :all, :include => :Content, :order => "created_at DESC"
被视为 ID,这就是 rails 抱怨的原因。
相反,要获得 all 条记录(没有约束),请使用恰当命名的 all method。你的代码应该是这样的:
@entries = Entry.includes(:content).order('created_at DESC').all
更新
显然,Content
是 Entry
模型上的一个属性的名称,因此 this 实际上应该是这样的:
@entries = Entry.order('created_at DESC').all
为了将来参考,您真的应该按照 ruby 代码的习惯用小写字母命名您的属性。 IE。
class Entry
attr_accessor :content, :title
end
相对于
class Entry
attr_accessor :Content, :Title
end
祝你升学顺利:-)
如果您使用的 rails 版本大于 4,那么 find
方法只需要 id
或多个 ids
作为参数以便获取所有行,您可以这样做:
def view_all
@entries = Entry.all.includes(:Content).order(created_at: :desc)
end
或者对于旧版本,您可以按照问题评论中的建议进行操作