Q&A: How to include a Rails text field without a variable?
20070206
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" />