Get updates

Scott Q&A: How to include a Rails text field without a variable?

The form helpers in Rails are great. But what do you do when you want to collect some data that’s not tied directly to a variable in your model?

If you went ahead and used the standard form helpers like I did on my first attempt, you’ll probably be greeted with an exception. The same Rails magic that injects a variable’s value into the field balks when your field name doesn’t reference an existing variable.

Instead Rails offers its helper for a standard, unattached text field; the name need not have anything to do with a model variable:

<% text_field_tag('after_this', '1', :size => '4', :maxlength => '4') %>

Producing markup like this:

<input id="after_this" maxlength="4" name="after_this" size="4" type="text" value="1" />

Leave a Reply


Fatal error: Uncaught Error: Call to undefined function show_subscription_checkbox() in /home/dmetro/3dmdesign.com/wp-content/themes/hex_columns/comments.php:90 Stack trace: #0 /home/dmetro/3dmdesign.com/wp-includes/comment-template.php(1472): require() #1 /home/dmetro/3dmdesign.com/wp-content/themes/hex_columns/single.php(65): comments_template() #2 /home/dmetro/3dmdesign.com/wp-includes/template-loader.php(74): include('/home/dmetro/3d...') #3 /home/dmetro/3dmdesign.com/wp-blog-header.php(19): require_once('/home/dmetro/3d...') #4 /home/dmetro/3dmdesign.com/index.php(17): require('/home/dmetro/3d...') #5 {main} thrown in /home/dmetro/3dmdesign.com/wp-content/themes/hex_columns/comments.php on line 90