Search code examples
ruby-on-railscheckboxparameterssimple-form

How to pass params through a checkbox tag


I have a simple form with a checkbox. I'm trying to pass a sale_id through the checkbox. However in the controller, the sale_id is not in the params hash.

I have tried different (unsuccessful) ways below:

<%= ff.check_box :refund, sale_id: sale.id, checked: true %>

<%= ff.check_box :refund, data: {sale_id: sale.id}, checked: true %>

Solution

  • If you want to pass in a single sales ID you can assign arbitrary values to the checked/unchecked states by passing them in as the 3rd and 4th arguments (after the options) e.g.

    <%= ff.check_box :refund, {}, sale.id, nil %>
    

    If it's an array of things to refund, then you can pass in multiple: true as an option, e.g.

    <%= ff.check_box :refund, {multiple: true}, sale.id, nil %>
    

    See the docs for more info on check_box.

    As some of the commenters have noted - it's very unclear why you would want to do this and there may be a better pattern/approach. It'd be helpful if you included what you're trying to do and how you want to use the value in the controller/model.