Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/wcf/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 3.1 Rails 3.1-从视图中删除查询并在控制器或模型中处理它的正确方法_Ruby On Rails 3.1_Rails Activerecord_Erb - Fatal编程技术网

Ruby on rails 3.1 Rails 3.1-从视图中删除查询并在控制器或模型中处理它的正确方法

Ruby on rails 3.1 Rails 3.1-从视图中删除查询并在控制器或模型中处理它的正确方法,ruby-on-rails-3.1,rails-activerecord,erb,Ruby On Rails 3.1,Rails Activerecord,Erb,我认为: <% @top_posts.each do |post| %> <li> <%= post.title %><br /> &nbsp;&nbsp; <%= link_to "Most popular comment", comment_path( post.comments.order("vote_cnt DESC").first ) </li> <% end %>

我认为:

<% @top_posts.each do |post| %>
  <li>
    <%= post.title %><br /> &nbsp;&nbsp;
    <%= link_to "Most popular comment", comment_path( post.comments.order("vote_cnt DESC").first )
  </li>
<% end %>



  • 我要做的是在我的
    Post
    模型中添加一个方法,根据您的标准获取第一条注释。然后,该视图看起来像
    comment\u path(post.relevant\u comment)

    很有意义,而且非常直接。谢谢