【译】把代码移到控制器中

Sunday, July 25, 2010

原文:http://rails-bestpractices.com/posts/24-move-code-into-controller

根据MVC的架构原理,在视图层不应该有逻辑代码出现,在这个实例中,我将介绍把一些代码从视图移到控制器

丑陋的...

<% @posts = Post.find(:all) %>
<% @posts.each do |post| %>
  <%=h post.title %>
  <%=h post.content %>
<% end %>

重构...

class PostsController < ApplicationController
  def index
    @posts = Post.find(:all)
  end
end

现在我们把finder移到了控制器中, 直接在视图中使用@posts就可以了

This entry was tagged Rails and Best-Practices

comments powered by Disqus

© 2009-2013 lxneng.com. All rights reserved. Powered by Pyramid

go to Top