dibakar
dibakar

Reputation: 19

Get save checkbox values in haml even if submit button not selected

This is SaaS ("Rotten Potatoes") training question that has been discussed many times last year but I don't seem to find answer to this.

I have a form with checkboxes for ratings that generate hash named ratings. Then I uses params[:ratings] to pass latest checkbox selections to different pages. My code works correctly when user submits the form. But if they wander around without clicking the submit button then the latest values are lost.

I think that the latest values needs to saved whenever user checks/unchecks a checkbox, but don't know how.

I need to do this using haml and ruby, no javascript etc.

Upvotes: 1

Views: 2022

Answers (2)

dibakar
dibakar

Reputation: 19

For the sake of completeness.

Requirement was that if the user closes the web page and comes back later then the previous selections should be available. So I did it with cookies and passed the test case.

Upvotes: 1

vladCovaliov
vladCovaliov

Reputation: 4403

In you're movies controller you have the instance variable @selected_ratings where you save all the current ratings.

if params[:ratings] != session[:ratings] and @selected_ratings != {}
  session[:ratings] = @selected_ratings
  redirect_to :sort => sort, :ratings => @selected_ratings and return
end
@movies = Movie.find_all_by_rating(@selected_ratings.keys, ordering)

And in the movies index, in the form_tag you have

= check_box_tag "ratings[#{rating}]", 1, @selected_ratings.include?(rating)

Upvotes: 1

Related Questions