提前道歉,我是一个新手,试图了解 rails 。
当我使用时,底部的 View 可以工作:
def show
@posts = Post.all
end
但是在我的 Controller 中我现在有:
def show
@posts = Post.find_by_category_id params[:id];
end
在我看来,我有
<%= @posts.each do |post| %>
<%= post.title %>
<% end %>
请解释一下为什么我会收到此错误。我应该用什么。 category_id 是 Post 表上的外键。
最佳答案
看看http://api.rubyonrails.org/classes/ActiveRecord/FinderMethods.html#method-i-find_by
Finds the first record matching the specified conditions
find_by_ 将仅返回一篇文章,而不是一个集合。所以你不能使用每一个。
关于ruby-on-rails - 未定义的方法 'each' Ruby on Rails,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31464935/