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/ Stack trace: #0 /home/dmetro/ require() #1 /home/dmetro/ comments_template() #2 /home/dmetro/ include('/home/dmetro/3d...') #3 /home/dmetro/ require_once('/home/dmetro/3d...') #4 /home/dmetro/ require('/home/dmetro/3d...') #5 {main} thrown in /home/dmetro/ on line 90