Ruby on Rails Collect 返回封闭数组(变量:数组:类的未定义方法“model_name”)
我有一个控制器,它返回我网站的所有文章
@articles = Article.find(all)
和用于渲染 @articles 数组的部分文章。
我已将控制器更改为:
@articles = User.find(1).topics.map { |t| t.articles }
所以我也可以返回一些其他数据
在 Rails Console 上检查后,我发现问题是collect的输出数组与Article.find(all)
find(的输出数组 不匹配) all)
[#<Article id: 1, user_id: 2, title: "test">]
collect 的输出数组
[[#<Article id: 1, user_id: 2, title: "test">]]
当我尝试渲染 parcial 时,我得到:
variable:undefined method `model_name' for Array:Class
My Index
<%= render :partial => @articles%>
然后是 parcial:
<%= link_to_unless_current h(article.title), article %> <%= h(article.body) %>
有谁知道如何克服数组的双括号 [[ ]] 的问题?
I had a controller that was returning all the articles of my website
@articles = Article.find(all)
and a partial that used to render the @articles array.
I have changed my controller to :
@articles = User.find(1).topics.map { |t| t.articles }
So I can return some other data as well
After inspection on the Rails Console I found out that the problem is that the output array of collect does not match the Article.find(all)
Output array of find(all)
[#<Article id: 1, user_id: 2, title: "test">]
Output array of collect
[[#<Article id: 1, user_id: 2, title: "test">]]
When I'm trying to render the parcial i get:
variable:undefined method `model_name' for Array:Class
My Index
<%= render :partial => @articles%>
and then the parcial:
<%= link_to_unless_current h(article.title), article %> <%= h(article.body) %>
Does anyone knows how to overcome the problem with the double brackets [[ ]] of the array?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
首先,对于第一行,我认为您有一个拼写错误,应该是
:all
而不是all
:Dt.articles
返回您文章集合。所以
map {|t| t.articles}
为您提供文章集合的集合(数组的数组)。你可以试试这个:
First, for the first line, I think you have a typo that should be
:all
instead ofall
:Dt.articles
returns you a collection of articles.So
map {|t| t.articles}
gives you a collection of collections of articles (the array of arrays).You could try this: