Avoiding Clutter in views


I have seen that when using rails for big applications especially the
with a lot of dynamic content. for example: profile pages in social
apps, newspaper websites etc.

The Views in such rails apps seem to have a lot of clutter, lots of
statements, ‘if’ statements etc.etc. Is there a way to avoid it or
way to do it.


Hi Rishav,

The cleanest way is to try and move as much logic into the models and
controllers( depending on the context) and then moving a lot of the
repetitive view logic into partials.

There is an excellent video on this on RailsCasts

Hope this helps.


Don’t forget about helpers. Helpers that take blocks are especially
useful, it’s a really easy way to conditionally display some content
without cluttering the view.


def is_authorized(action, resource, &block)
yield if current_user.permission_check(action, resource)


<% is_authorized(:edit, @user) do %>
<%= link_to “Edit”, edit_user_path(@user) %>
<% end %>

What are the performance impacts of these various techniques?

Has anyone managed or reduced complexity using layouts in any
clever ways?


Hi guys,

Thanks for the replies. I have tried a bunch of the things mentioned
above but still even with the partials and moving code to the
helpers,I dont like the result.
May be I haven’t done things right. But humor me for a thought I had
when thinking about the solution…
I have simple profile page filled up with content from different
models and the page is full of “render” statements. Is there a more
CMS type solution here.

Like for example in Drupal,(aside from the fact its a little
inflexible) , manages views in a very clean and decoupled manner
from the actual application. It delegates the page content to php
variables Like “side-bar” “content” “footer” which acts like a base
theme and then you can have separate sub themes managing individual
components. The sub themes are combined and put in place in the base
theme to render the final output without (atleast most of the time)
the user telling Drupal.

Can anyone recommend any good Rails / Ruby based CMS?



You should check out Radiant CMS. It’s Rails-based and has the same
type of content parts to the pages that you’re describing from Drupal.


I’ll check it out