Selvam
Selvam

Reputation: 1093

Rails-Haml, Table display of checkbox selection

I have a table that displays data for the items selected in the checkbox above, like a[], b[], c[]. Initially, when the page is first loaded, the table display is empty. When checkbox selections are made, the tables displays the data for the selection. Then when I untick all checkboxs and press display, the table display should be empty.

In the controller the variable for checkbox selection is thus>

@selected_brands = params[:brands] || session[:brands] || {}

as you may realize, params[], and session[] are values submitted by form in the view. {} -> when no checkboxes are selected. Again in controller, I have this @products = Product.find_all_by_brand(@selected_brands.keys)

The problem I have is, when I have unchecked the boxes, following the previous selections I made, the table displays data from previous session, instead of an empty table. What am I not doing?

Upvotes: 0

Views: 701

Answers (1)

Hugo Logmans
Hugo Logmans

Reputation: 2252

You run into a HTML issue bugging lots of people: when you uncheck all checkboxes, nothing is sent to the server, and thus no change is recorded, so the last situation is still recorded.

There are many solutions to this (events, javascript, serverside code), so my advise is to broaden your search to generic html how to handle this situation best.

If you don't like Javascript, and have no problem with too many db-actions, remove all selected brands for the product and reapply the chosen ones when you get this result.

Upvotes: 1

Related Questions