Fork me on GitHub

Move code into controller

24 Jul 2010

Wen-Tien Chang

Bad Smell

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

According to MVC architecture, there should not be logic codes in view. The posts finder should not exist in view, please move it into controller.

Refactor

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

Now we move the posts finder into controller, we can use the @posts directly in view.

Tags