"Persisting" Forms


#1

I am building a search form for my application that is not directly
tied to a model.

I would like to search results page to display the original search
form with the inputted values along with the results.

I have everything in order, but when the form is posted, the form on
the results page is set back to the defaults. I have figured out how
to get my text_field_tag to keep the appropriate value, but is there
an easy way to do this with selection lists (using select_tag) to do
this as well?

Thank you for your assistance,

Michael


#2

Michael G. wrote:

this as well?
If your form is tied to a model object (that means you use <%= select
‘object’, ‘method’ %> helpers) you can just pass the filled @object
variable back to your view, and it will display the filled in values.


#3

My form actually wasn’t tied to a model. I ended up overcoming the
issue by using the select_tag and building the options via code.

No big deal, really. Just needed to be in the right state of mind.

Thanks,

Michael


#4

Michael G. wrote:

My form actually wasn’t tied to a model. I ended up overcoming the
issue by using the select_tag and building the options via code.

No big deal, really. Just needed to be in the right state of mind.

Hmm, I use my own objects like:

class FormValues
attr_accessor :field1, :field2

 def initialize(p = {})
     field1 = p[:field1] || 'default value'
     field2 = p[:field2] || 'default value'
     # ...
 end

end

@form = FormValues.new # to load it with default values
@form = FormValues.new(params[:form]) # to load it with posted values


Kamil


#5

Now that makes a lot of sense. LOTS more than my method.

In Rails, where do you define your extra classes? Do I create an .rb
file in my models directory, or is this a helper?


#6

That would go in the models directory. It’s very handy to use
non-persisted
model objects like this.

-Tom


#7

I guess that would be the lib directory