# DRYing up the view

**URL:** <https://www.ruby-forum.com/t/drying-up-the-view/101035>\
**Category:** Rails\
**Created:** [August 3, 2006, 3:04am UTC](https://www.ruby-forum.com/t/drying-up-the-view/101035 "2006-08-03T03:04:20Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Igor](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/dec6dc/32.png) [@Igor](https://www.ruby-forum.com/u/Igor)\
**Post date:** [August 3, 2006, 3:04am UTC](https://www.ruby-forum.com/t/drying-up-the-view/101035/1 "2006-08-03T03:04:20Z")

</div>

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? 🙂  
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!

---

<div class="post-metadata">

**Author:** ![Igor](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/dec6dc/32.png) [@Igor](https://www.ruby-forum.com/u/Igor)\
**Post date:** [August 3, 2006, 6:04am UTC](https://www.ruby-forum.com/t/drying-up-the-view/101035/2 "2006-08-03T06:04:24Z")

</div>

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](http://dereksivers.com/rails-shared-controller.html)  
a good way for DRYing the CRUD controllers or there are better ways?

Thanks!

---

<div class="post-metadata">

**Author:** ![Igor](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/dec6dc/32.png) [@Igor](https://www.ruby-forum.com/u/Igor)\
**Post date:** [August 3, 2006, 4:28pm UTC](https://www.ruby-forum.com/t/drying-up-the-view/101035/3 "2006-08-03T16:28:07Z")

</div>

On 8/3/06, Michael M. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > By the way is this [Derek Sivers](http://dereksivers.com/rails-shared-controller.html)  
> > 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…

---

<div class="post-metadata">

**Author:** ![Igor](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/dec6dc/32.png) [@Igor](https://www.ruby-forum.com/u/Igor)\
**Post date:** [August 3, 2006, 7:52am UTC](https://www.ruby-forum.com/t/drying-up-the-view/101035/4 "2006-08-03T07:52:49Z")

</div>

> By the way is this [Derek Sivers](http://dereksivers.com/rails-shared-controller.html)  
> 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

---

<div class="post-metadata">

**Author:** ![Igor](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/dec6dc/32.png) [@Igor](https://www.ruby-forum.com/u/Igor)\
**Post date:** [August 4, 2006, 12:53pm UTC](https://www.ruby-forum.com/t/drying-up-the-view/101035/5 "2006-08-04T12:53:53Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Igor](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/dec6dc/32.png) [@Igor](https://www.ruby-forum.com/u/Igor)\
**Post date:** [August 15, 2006, 2:11pm UTC](https://www.ruby-forum.com/t/drying-up-the-view/101035/6 "2006-08-15T14:11:48Z")

</div>

On 8/2/06, Igor Su [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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](http://dereksivers.com/rails-shared-controller.html)  
> 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](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
