How to use hidden values

So…say you’re in RoR and you want to pass to yourself some specific value to a hidden part of a form? Something that doesn’t totally match a DB entry, in the name?

Here’s how:

(This shows you how to specify the starting value, which also applies to other types besides hidden fields, BTW)

<code>

<%= hidden_field ‘filter’, “id”, :value => “newvalue” %>

</code>
This generates filter[id] = newvalue in the HTML.

What this actually does is allows you to setup an “arbitrary” value to pass into the controller.  Typically with RoR

<code>

<%= hidden_field ‘filter_to_use’, “id” %>

</code>

Generates <code>filter_to_use[id] = filter_to_use.id </code> in the HTML (I think). This is of course not useful if filter_to_use is a variable name that is just a variable you want to pass in to the next controller (i.e. it doesn’t exist).  So you need to use the <code> :value => whatever </code>above.

To use it in the receiving controller:

@filter = Filter.find(params[:filter_to_use][:id]) # retarded
What this does is looks up your parameter. So that’s a little contorted, but works, once you know how to set the value.

Leave a Reply

Your email address will not be published. Required fields are marked *

Time limit is exhausted. Please reload the CAPTCHA.