# Questions about inheritance in Rails

**URL:** <https://www.ruby-forum.com/t/questions-about-inheritance-in-rails/106117>\
**Category:** Rails\
**Created:** [June 6, 2007, 4:29pm UTC](https://www.ruby-forum.com/t/questions-about-inheritance-in-rails/106117 "2007-06-06T16:29:11Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![s0nspark](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/f475e1/32.png) [@s0nspark](https://www.ruby-forum.com/u/s0nspark)\
**Post date:** [June 6, 2007, 4:29pm UTC](https://www.ruby-forum.com/t/questions-about-inheritance-in-rails/106117/1 "2007-06-06T16:29:11Z")

</div>

I am in the process of subclassing some things in a current Rails  
project and would like some feedback on how I’m approaching it… what I  
have works but I’m not sure if there is a better way to approach this.  
🙂

I am currently subclassing controllers like so:

class LookupsController \< ApplicationController

# GET /lookups

def index  
end

#-----------------------------------------------------------------

# the rest of these methods are called from subclassed controllers

#-----------------------------------------------------------------

# GET /{controller\_name}

# GET /{controller\_name}.xml

def index\_list  
render ‘lookups/index’ and return if controller\_name == ‘lookups’  
eval(’@’ + controller\_name + ’ = ’ + model\_name + ‘.find(:all)’)  
respond\_to do |format|  
format.html { render ‘lookups/list’ }  
format.xml { render :xml =\> eval(’@’ + controller\_name +  
‘.to\_xml’) }  
end  
end

# … and so on …

end

class StatesController \< LookupsController  
def index  
index\_list  
end  
end

and the shared view template looks like this;

# \<%= pretty\_controller\_name %\>
 \<% for item in eval('@' + controller\_name) %\> "\> \<% end %\> 

| Actions | Name |
| &nbsp; \<%= link\_to(image\_tag('item\_delete.gif'), eval(model\_name + '\_path(:id =\> item)'), :confirm =\> 'Are you sure?', :method =\> :delete, :class =\> 'image\_link') %\> \<%= link\_to(image\_tag('item\_edit.gif'), eval('edit\_' + model\_name + '\_path(:id =\> item)'), :class =\> 'image\_link') %\> | \<%= item.name %\> (\<%= pluralize(item.jobs.count, "job") %\>) |

  
\<%= submit\_tag "Add a " + pretty\_model\_name, {:type =\> 'button', :onClick=\>"parent.location='" + eval('new\_' + model\_name + '\_path') + "'" } %\>

and a helper like so:

module LookupsHelper  
def controller\_name  
controller.controller\_name  
end  
def model\_name  
controller.controller\_name.singularize  
end  
def pretty\_model\_name  
model\_name.gsub(’_’, ’ ‘).capitalize\_all  
end  
def pretty\_controller\_name  
controller\_name.gsub(’_’, ’ ').capitalize\_all  
end  
end

My main question revolves around the liberal use of eval() … is there  
a better way? Am I making this harder than it should be?

Thanks for any feedback…

Cheers,  
Tim

---

<div class="post-metadata">

**Author:** ![s0nspark](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/f475e1/32.png) [@s0nspark](https://www.ruby-forum.com/u/s0nspark)\
**Post date:** [June 6, 2007, 4:39pm UTC](https://www.ruby-forum.com/t/questions-about-inheritance-in-rails/106117/2 "2007-06-06T16:39:42Z")

</div>

Hi –

On Wed, 6 Jun 2007, Tim F. wrote:

> # GET /lookups
> 
> render ‘lookups/index’ and return if controller\_name == ‘lookups’  
> eval(‘@’ + controller\_name + ’ = ’ + model\_name + ‘.find(:all)’)  
> respond\_to do |format|  
> format.html { render ‘lookups/list’ }  
> format.xml { render :xml =\> eval(‘@’ + controller\_name +  
> ‘.to\_xml’) }  
> end  
> end

This is all sort of at first glance, so I could well be overlooking  
some pitfalls, but why don’t you just say @items or something instead  
of all the eval’ing? Also, instead of model\_name, you could have your  
helper method return a model class. That way, you could do:

@items = model.find(:all)

> \<% for item in eval(‘@’ + controller\_name) %\>
> 
> "\> &nbsp; \<%= link\_to(image\_tag('item\_delete.gif'), eval(model\_name + '\_path(:id =\> item)'),

At the very least I would use send there:

send(“#{model\_name}\_path”, :id =\> item)

But it’s nicer if you can encapsulated that kind of calculation in the  
controller or a helper method.

I’ll leave it at that, pending feedback on what I’ve missed 🙂

David

–  
Q. What is THE Ruby book for Rails developers?  
A. RUBY FOR RAILS by David A. Black ([Ruby for Rails](http://www.manning.com/black))  
(See what readers are saying! [http://www.rubypal.com/r4rrevs.pdf](http://www.rubypal.com/r4rrevs.pdf))  
Q. Where can I get Ruby/Rails on-site training, consulting, coaching?  
A. Ruby Power and Light, LLC ([http://www.rubypal.com](http://www.rubypal.com))

---

<div class="post-metadata">

**Author:** ![s0nspark](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/f475e1/32.png) [@s0nspark](https://www.ruby-forum.com/u/s0nspark)\
**Post date:** [June 6, 2007, 4:54pm UTC](https://www.ruby-forum.com/t/questions-about-inheritance-in-rails/106117/3 "2007-06-06T16:54:05Z")

</div>

David wrote:

> This is all sort of at first glance, so I could well be overlooking  
> some pitfalls, but why don’t you just say @items or something instead  
> of all the eval’ing? Also, instead of model\_name, you could have your  
> helper method return a model class. That way, you could do:
> 
> @items = model.find(:all)

Thanks… good point. 🙂 I was so focused on translating the existing  
code that I missed that.

> At the very least I would use send there:
> 
> send("#{model\_name}\_path", :id =\> item)
> 
> But it’s nicer if you can encapsulated that kind of calculation in the  
> controller or a helper method.

In other words, wrap the route method invocation with a helper method to  
simplify the view code?

One other question comes to mind… in the view I am creating a  
button…

\<%= submit\_tag "Add a " + pretty\_model\_name, {:type =\> ‘button’,  
:onClick=\>“parent.location=’” + eval(‘new\_’ + model\_name + ‘\_path’) +  
“’” } %\>

Is there a cleaner way to do this besides building the onClick  
javascript?

> I’ll leave it at that, pending feedback on what I’ve missed 🙂

Thanks much for the feedback!

Cheers,  
Tim

---

<div class="post-metadata">

**Author:** ![s0nspark](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/f475e1/32.png) [@s0nspark](https://www.ruby-forum.com/u/s0nspark)\
**Post date:** [June 6, 2007, 4:58pm UTC](https://www.ruby-forum.com/t/questions-about-inheritance-in-rails/106117/4 "2007-06-06T16:58:48Z")

</div>

Hi –

On Wed, 6 Jun 2007, Tim F. wrote:

> > 
> 
> In other words, wrap the route method invocation with a helper method to  
> simplify the view code?

Yes – something like:

def model\_path(\*args)  
send(“#{model\_name}\_path”, \*args)  
end

at which point you could do:

\<%= link to “Click here”, model\_path :id =\> item %\>

There may be yet further ways to simplify it all but initially at  
least I’d go for pushing all the low-level-ish stuff out of the views  
and see what happens.

(Punting on the JS question… 🙂

David

–  
Q. What is THE Ruby book for Rails developers?  
A. RUBY FOR RAILS by David A. Black ([Ruby for Rails](http://www.manning.com/black))  
(See what readers are saying! [http://www.rubypal.com/r4rrevs.pdf](http://www.rubypal.com/r4rrevs.pdf))  
Q. Where can I get Ruby/Rails on-site training, consulting, coaching?  
A. Ruby Power and Light, LLC ([http://www.rubypal.com](http://www.rubypal.com))
