Avoiding Clutter in views

Hi,

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

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

Thanks,
Rishav

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
http://railscasts.com/episodes/55-cleaning-up-the-view

Hope this helps.

Rob

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.

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

# View
<% 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 particularly clever ways?

al

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?

Thanks
Rishav

Rishav,

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.

~Thadd

I'll check it out

Thanks
Rishav