简体   繁体   中英

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.

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM