I have a search form in a view.
<%= form_with url: issues_path, class: 'filter-box' do %>
<%= text_field_tag(:filter_by, '', id: 'filter-text-field') %>
<% end %>
When the form is submitted, the following action is run:
class IssuesController < ApplicationController
...
def create
@labels = Label.by_search_term(params[:filter_by])
respond_to do |format|
format.js
end
end
end
All works well.
However, I want the action to be triggered with every keystroke. To that end, I have added the following javascript.
<script>
document.getElementById('filter-text-field').addEventListener('keyup', function(){
document.querySelector('.filter-box').submit()
})
</script>
But when the form is submitted on key up it breaks and I get an error: ActionController::UnknownFormat in IssuesController#create
.
Looking at the params, I don't see anything notable.
# Submitted by pressing return
# => <ActionController::Parameters {"utf8"=>"✓", "authenticity_token"=>"TJRajeBbEK8+D0Ly5/lbI/SVx8srhG/YS2W1l+Zc+f+TYmsbfehXnsluxOovblCrWBLJy0exhyzhsY+qPhBDOQ==", "filter_by"=>"d", "controller"=>"issues", "action"=>"create"} permitted: false>
# Submitted on keyup
# => <ActionController::Parameters {"utf8"=>"✓", "authenticity_token"=>"7NBwqCuJpbFGvxWgq6qqdB1hF0yptca0N446G4nQ620zJkE+tjrigLHek7hjPaH8seYZTMWALkCdWgAmUZxRqw==", "filter_by"=>"d", "controller"=>"issues", "action"=>"create"} permitted: false>
What is the difference between submitting the form by pressing enter and submitting the form via javascript? What do I need to do to ensure it works correctly?