Noob Question: REgarding Forms ( Take 2 )


#1

I’m hoping there’s an easy way to do this. I’m trying to read options
from a database, and insert them into form tags. So for example,

In the Database Table: options

size: 300
type: text_field
maxsize: 300

and I want to create a text_field form tag, with all the appropriate
options, is there an easy way to do this, other than having the code
output the HTML directly? I know I could just have the view put out;

<input name= etc…

but I want to use the Ruby Form helpers.

<%= text_field %>

I’m assuming I could write methods that compare what’s in the DB with
certain text, and then output a text_field or radio_button for example,
I was hoping I could just grab from the DB, and post it into the view.

Basically, I want to use database info, and input it into Ruby code to
be interpreted by ruby… i think :stuck_out_tongue:

Hope that made sense, thanks in advance


#2

Randal S. wrote:

I’m hoping there’s an easy way to do this. I’m trying to read options
from a database, and insert them into form tags. So for example,

In the Database Table: options

size: 300
type: text_field
maxsize: 300

and I want to create a text_field form tag, with all the appropriate
options, is there an easy way to do this, other than having the code
output the HTML directly? I know I could just have the view put out;

<input name= etc…

but I want to use the Ruby Form helpers.

<%= text_field %>

I’m assuming I could write methods that compare what’s in the DB with
certain text, and then output a text_field or radio_button for example,
I was hoping I could just grab from the DB, and post it into the view.

Basically, I want to use database info, and input it into Ruby code to
be interpreted by ruby… i think :stuck_out_tongue:

Hope that made sense, thanks in advance

So each row of your table represents a form field? Then you can do
something like this:

<% for field in @form_fields %>
<%= send field.type, ‘model’, ‘method’, :size => field.size, :maxsize
=> field.maxsize %>
<% end %>

The trick here is that since you can’t call the form_helper method
directly since you don’t know which method use yet, you use “send” to
execute a method defined by a symbol or string.

So if field.type is “text_field” then

send field.type, *attrs #is the same as:
send “text_field”, *attrs #is the same as:
text_field *attrs

Although the form helper still must take ‘model’ and ‘method’ parameters
just like a standard call to text_field. If you want to use the
“text_field_tag” type of methods that are not bound to an instance
variable, then use:

send “#{field.type}_tag”, *attrs


#3

Thanks Alex, the SEND command is exatly what I was looking for!

Much obliged, I owe you one!

Cheers!

Alex W. wrote:

So each row of your table represents a form field? Then you can do
something like this:

<% for field in @form_fields %>
<%= send field.type, ‘model’, ‘method’, :size => field.size, :maxsize
=> field.maxsize %>
<% end %>

The trick here is that since you can’t call the form_helper method
directly since you don’t know which method use yet, you use “send” to
execute a method defined by a symbol or string.

So if field.type is “text_field” then

send field.type, *attrs #is the same as:
send “text_field”, *attrs #is the same as:
text_field *attrs

Although the form helper still must take ‘model’ and ‘method’ parameters
just like a standard call to text_field. If you want to use the
“text_field_tag” type of methods that are not bound to an instance
variable, then use:

send “#{field.type}_tag”, *attrs