1

When I create multiple buttons for the same form in Rails, I can use the value of param[:commit] to figure out which button was pushed.

How do I create multiple submit buttons for the same form in Rails?

However, I have a search results page where a user can further refine the search, updating the results by selecting or deselecting facets, changing date filters, etc. I'd like to be able to trigger a form.submit() using javascript when a facet checkbox is checked/unchecked, for example. How do I pass a parameter (such as the param[:commit] mechanism) to my controller so that I know which user action triggered the refresh. I don't want to make everything a button.

$('.facet').on('change', function(e) {

    // Do some stuff

    var $form = $(this).closest('form');
    $form.submit();  // I want to pass in a "facet-selected" here so I know what triggered the submit

});

With a controller method that looks like this

def refresh_search
    if params[:commit] == 'facet-selected'
        # Do something
    elsif params[:commit] == 'date-range-changed'
        # Do something else
    end
end

Thanks so much in advance.

Christy

Community
  • 1
  • 1
user2437059
  • 63
  • 1
  • 4

1 Answers1

2

I suppose you should change form a bit, add hidden input same name as submit buttons and set it value when submit form by JS. E.g.

<form>
    <input type="hidden" name="commit" />    
    <input type="submit" name="commit" value="date-range-changed" />    
</form>

Script

var $form = $(this).closest('form');
$form.find(':hidden').val('facet-selected');
$form.submit();