DRYing up the view

Hello everyone!
I have generic views for CRUD operations
For example I render(‘shared/list’) from controller

shared/list.rhtml

<% for item in @list %>

<%= render :partial => 'item', :locals => { :item => item } %> <%= link_to 'Edit', :action => :edit, :id => item %> <%= link_to 'Destroy', { :action => :destroy, :id => item }, :confirm => "Delete: #{item.name} ?", :post => true %> <% end %> ...

As you see I also need to render :partial => ‘list’ on every iteration
to add some custom information for a particullar controller.

districts/_item.rhtml

<%= item.name %> <%= item.region.name %> ...

As you may already guess I’m getting ActionController::DoubleRenderError
So is there better way of doing this, or maybe some other function
method such as: render_and_not_raise_DoubleRenderError exist? :slight_smile:
Any ideas?

Someone proposed to use render_to_string, but it does not work in views.
Or creating an array with items in controller that will be used in
template to populate the view. But its not MVC way of doing things.

I mean common! It should be simple, as a method call!

Thanks for help!

Its all working now!
Sorry, my bad!
It was hapening bacause I didn’t remove render method from my
controller.
This method was called first and then one from generic controller

By the way is this Derek Sivers
a good way for DRYing the CRUD controllers or there are better ways?

Thanks!

On 8/3/06, Michael M. [email protected] wrote:

By the way is this Derek Sivers
a good way for DRYing the CRUD controllers or there are better ways?
That is very cool! I’m curious to know if any of the senior railers out
there endorse that methodology! It looks very promising.

By putting view information into the model (in the ‘Nub’), you might
be committing the cardinal sin of mixing Model and View, and you may
find yourself banished into MVC purgatory to consider what you’ve
done…

By the way is this Derek Sivers
a good way for DRYing the CRUD controllers or there are better ways?

That is very cool! I’m curious to know if any of the senior railers out
there endorse that methodology! It looks very promising.

Michael

use the render partial collection counter to do your style alternation

<%= render :partial => “listitem”, :collection => @list %>

provides you with listitem_counter to be able to use for alternating
style in your partial like

...

You would put everything you need into the partial, if in your case
the partial is shared and you need to use a partial elsewhere without
the links then you might use another partial which includes this
partial or simply some conditional logic around parts of it.

On 8/2/06, Igor Su [email protected] wrote:

Its all working now!
Sorry, my bad!
It was hapening bacause I didn’t remove render method from my
controller.
This method was called first and then one from generic controller

By the way is this Derek Sivers
a good way for DRYing the CRUD controllers or there are better ways?

I hadn’t seen that before and was pretty impressed by it as well.
Taking inspiration from Derek’s example, I implemented something
similar as a mixin module. I’m not sure it’s a good way of doing it,
and it’s very rough, but it seems to work OK so far:

http://rpaste.com/pastes/355

It doesn’t require you to define a ‘model’ method because it guesses
it automatically based on the controller it’s mixed in with (somewhat
inelegantly):

def model
self.class.to_s.match(/^(.*)Controller$/)[1].constantize
end

So all you have to do is something like:

class DomainController < ApplicationController
include InstantCrud
end

It also assigns instance variables in a nicer way, thanks to the
inflector. Given the controller above, you get @domains in your list
action and @domain in view and edit instead of @list, @edit etc.

Probably you’re better of going with the REST features in edge rails
though. That said, I’d like to know whether or not this is a horrible
way of implementing something like this.

-Pawel