# View Inheritance Population, and another question

**URL:** <https://www.ruby-forum.com/t/view-inheritance-population-and-another-question/143454>\
**Category:** Rails\
**Created:** [July 18, 2008, 7:45pm UTC](https://www.ruby-forum.com/t/view-inheritance-population-and-another-question/143454 "2008-07-18T19:45:03Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![aawilson](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/e495f1/32.png) [@aawilson](https://www.ruby-forum.com/u/aawilson)\
**Post date:** [July 18, 2008, 7:45pm UTC](https://www.ruby-forum.com/t/view-inheritance-population-and-another-question/143454/1 "2008-07-18T19:45:03Z")

</div>

I’m trying to create a select box that chooses from a list of options  
determined by class hierarchy. To be specific, I have a Field model  
that has been subclassed into several types of Fields. The user chooses  
a type of field from the drop-down, and that class is used to generate  
the Field back in the controller. I have two questions related to this.

1. In attempting to populate the select box, I created a helper  
function that searches the ObjectSpace for children of the Field class.  
Unfortunately, within the view in question, only objects named within  
the view are added to the scope by Rails, so when I call the helper  
function, it only finds said fields. I have worked around this problem  
by simply naming every member of the hierarchy in the view, but for this  
to work, I have to stick the entire list in every view that uses this  
drop-down, and if I ever add to the hierarchy, each instance of that  
list will have to be modified manually. I would like a better solution.

2. To actually generate the new field, I am calling something that  
looks like newfield = eval(params[:fieldtype] + “.create(” +  
“:first\_attrib =\>,” + “second\_attrib” + etc. + “)”. Eval is a rather  
scary function to be using, here, since it is probably fairly easy to  
generate a POST with some sort of injected code that the eval would  
execute. I could create helper functions to sanitize the parameters,  
but it seems like this would come up often enough that Rails should  
already have a solution, a solution that I am not experienced enough to  
locate. Does such a thing exist?

Thanks in advance for your time.

-A. Wilson

---

<div class="post-metadata">

**Author:** ![aawilson](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/e495f1/32.png) [@aawilson](https://www.ruby-forum.com/u/aawilson)\
**Post date:** [July 18, 2008, 8:03pm UTC](https://www.ruby-forum.com/t/view-inheritance-population-and-another-question/143454/2 "2008-07-18T20:03:36Z")

</div>

Re: #1, if all else fails, you should be able to put that list of  
subclass names in your model, and reference it from there. Something  
like:

class Field \< AR:Base  
SUB\_TYPES = %(bibbity bobbity boo rama lama ding dong)  
end

And in your views:

f.select(:field\_type, Field::SUB\_TYPES)

Having that array accessible should also help w/sanitizing. So e.g., in  
your controller

raise(“WTF!?”) unless Field::SUB\_TYPES.include?(params[:field\_type])

That’s air code, but you get the idea…

HTH,

-Roy

---

<div class="post-metadata">

**Author:** ![aawilson](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/e495f1/32.png) [@aawilson](https://www.ruby-forum.com/u/aawilson)\
**Post date:** [July 18, 2008, 8:23pm UTC](https://www.ruby-forum.com/t/view-inheritance-population-and-another-question/143454/3 "2008-07-18T20:23:47Z")

</div>

Roy P. wrote:

> Re: #1, if all else fails, you should be able to put that list of  
> subclass names in your model, and reference it from there. Something  
> like:
> 
> class Field \< AR:Base  
> SUB\_TYPES = %(bibbity bobbity boo rama lama ding dong)  
> end
> 
> And in your views:
> 
> f.select(:field\_type, Field::SUB\_TYPES)
> 
> Having that array accessible should also help w/sanitizing. So e.g., in  
> your controller
> 
> raise(“WTF!?”) unless Field::SUB\_TYPES.include?(params[:field\_type])
> 
> That’s air code, but you get the idea…
> 
> HTH,
> 
> -Roy

That’s quite beautiful, actually, and I may re-implement my temporary  
solution that way, mostly for the sake of #2. However, if I could get  
the list to populate itself automatically, that would be ideal.
