alv_721
alv_721

Reputation: 325

How do I keep form input data between AJAX requests (Rails)

I have trouble understanding the way of handling dynamic form in rails.

Lets say I have ordinary form which is generated dynamically by user manipulations with the selects and checkboxes on the top of the page (another, stand-alone form). So parts of my form are showing up and disappearing with AJAX requests. What I need is storing already filled in fields so even if they are re-rendered, user sees what was put in already.

Example:

Table row before request:

Field1 | FieldBlah | Field234
_____________________________
value1 |           | value234

Then I get Ajax request and table is reloaded, so I get:

Field1 | FieldBlah | Field234 | FieldXXX
________________________________________
       |           |          |

with blank fields.

How do I keep data so User no longer needs re-write data into same fields?

Upvotes: 0

Views: 525

Answers (1)

Ajay
Ajay

Reputation: 4251

In your case,you can carry forward your values in cookies. Cookies actually consists of a single name = value pair (both of name, pair must be text).

cookies["field_1"] = "value1"
cookies["field_234"] = "value_234"

If you just want to hold all the values (let's say you have got 10 fields and sequentially if you want to hold of all them) then you can do:

cookies['my_values'] = "#{value_1}_#{value_2}_#{value_3}_#{value_4}"

like this way, you can hold all 10values(may be blank value,if some field don't hold any value). then while splitting use '_'(underscore) to separate values(10 fields).

Hope you find this useful :)

Upvotes: 1

Related Questions