raphael_turtle
raphael_turtle

Reputation: 7314

Rails 4 ActionController::UnknownFormat format.js error

I have a search form on a users page

= form_tag(action: 'search', remote: true ) do
    = date_field_tag 'start'
    = submit_tag "Find" 

When I submit the form, I get the following error:

406 Not Acceptable in 4ms (ActiveRecord: 0.7ms)

    ActionController::UnknownFormat - ActionController::UnknownFormat:
      actionpack (4.2.5) lib/action_controller/metal/mime_responds.rb:219:in `respond_to'
      app/controllers/users_controller.rb:40:in `search'

My controller action is:

def search
    puts  params
    respond_to do |format|
      format.js
    end
end

I have a search.js.erb file that just prints to the console.

I can't figure out why this isn't working.

Upvotes: 1

Views: 1333

Answers (1)

Arslan Ali
Arslan Ali

Reputation: 17802

You are calling the method form_tag wrong way. If you specify action in form_tag, you must specify those in different hashes.

= form_tag( { action: 'search' }, { remote: true } ) do

And this is the reason that why you were getting request as an HTML request, instead of a JS request.

You will have to be explicit telling form_tag that you are sending action: 'search' for url_for_option hash, and remote: true for options hash.

Here's the method signature:

form_tag(url_for_options = {}, options = {}, &block)

For more information, head over to form_tag.

Upvotes: 1

Related Questions