Take the 2-minute tour ×

iam trying to save the current state of a form in the sessionhash (with ajax) and redisplay it after a pagerefresh oder reopened browserwindow:

def save
  session[:last_order] = params.dup
def new
  @last_state = session[:last_order] || {}

that was the easy part.

the problem is, when iam showing the form on the new action, the @last_state could be an empty hash, so how can i handle the form-tags without checking the existance of the key on every form-tag creation?

<%= radio_button_tag 'product_id', product.id, @last_state.has_key? 'product_id' && @last_state['product_id'] == product.id %>

i just want to write a simple line like this:

<%= radio_button_tag 'product_id', product.id, @last_state['product_id'] == product.id %>

or even better:

<%= radio_button_tag 'product_id', product.id, @last_state.product_id == product.id %>

secondary question: i heard of a rails-way to treat hashes like objects but i forgot that. can anyone show me the direction?

share|improve this question

1 Answer 1

up vote 0 down vote accepted

what about setting up a default state for @last_state

def new
  @last_state = session[:last_order] || {product_id: 0}
share|improve this answer

Your Answer


