title: Rails: Refactoring, views, helpers: how does it all go together?
body: <p>Warning: Noob here.</p>
  
  <p>I know this is a trivial subject but I'm having a lot of difficulty in figuring out how exactly I can simplify my views by moving parts of them into helpers. For example, I've always read that conditionals in your views are prime candidates for extraction into helpers, but I couldn't really find examples of this, and my attempts to achieve this failed.</p>
  
  <p>For example, suppose I have:</p>
  
  <pre><code>#index.html.erb
  
  &lt;% for beast in @beasts do -%&gt;
    &lt;% if beast.dead? -%&gt;
      &lt;%= beast.body %&gt;
      &lt;%= link_to "bury", bury_beast_path( :id =&gt; beast.id ) %&gt;
    &lt;% else -%&gt;
      &lt;%= beast.body %&gt;
      &lt;%= link_to "kill!", kill_beast_path( :id =&gt; beast.id ) %&gt;
    &lt;% end -%&gt;
  &lt;% end -%&gt;
  </code></pre>
  
  <p>It annoys me a little to have this in my view, but how exactly could I move this to a helper instead? And further simplify it, if possible. (I've read somewhere that conditionals are bad but it's just beyond me how you could program anything without them.)</p>
  
  <p>Another example: I need to <code>id</code> my <code>body</code> tags with the format <code>controller_action</code>. The best I've got so far is this:</p>
  
  <pre><code>#index.html.erb
  
  &lt;body id="&lt;%= controller_action %&gt;"&gt;
  </code></pre>
  
  <p>…and…</p>
  
  <pre><code>#application_helper.rb
  
  def controller_action
    @id = @controller.controller_name + "_" + @controller.action_name
  end
  </code></pre>
  
  <p>I'm no expert, but that's still ugly even to me. </p>
  
  <p>To make things more complicated, <a href="http://37signals.com/svn/posts/1952-i-gave-a-talk-on-ui-fundamentals-for-programmers" rel="noreferrer">Ryan Singer said something I liked</a>: to treat ERB like an image tag, using helpers to "reveal intention". Then in the next breath saying that you should have no HTML in helpers for that is the way to hell. WTF? How are both things compatible? If it's come to the point where you can just declare behaviors in the view, surely there should be a lot of HTML to be rendered behind the scenes? I can't grasp it.</p>
  
  <p>So, that's basically it. I'd appreciate if anyone could share some thoughts on this, or point me to some good in depth reading on the subject – which I've found to have a really weak coverage on the web. I've already googled it to exhaustion but who knows.</p>