Forum: Ruby on Rails Noob Question: REgarding Forms ( Take 2 )

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
6e03895e5abb46f3daf4aafad3b98d5c?d=identicon&s=25 Randal Santia (Guest)
on 2006-04-25 19:51
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 :P

Hope that made sense, thanks in advance
D0cd6b10e01bacb976b3b815a9c660bc?d=identicon&s=25 Alex Wayne (Guest)
on 2006-04-25 21:47
Randal Santia 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 :P
>
> 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
6e03895e5abb46f3daf4aafad3b98d5c?d=identicon&s=25 Randal Santia (Guest)
on 2006-04-25 22:55
Thanks Alex, the SEND command is exatly what I was looking for!

Much obliged, I owe you one!

Cheers!

Alex Wayne 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
This topic is locked and can not be replied to.