1

Why is my controller action only returning in HTML format even when I explicitly state that it can only return as of type JS?

app/controllers/classrooms_controller.rb

def create
  respond_to do |format|
    format.js # This doesn't work.
  end
end

app/views/classrooms/new.html.haml

= form_tag classrooms_path, :html => {:multipart => true}, do |f|
  = text_field_tag :name
  = submit_tag "Done"

app/views/classrooms/create.js.erb

alert('hi');

Server

Started POST "/classrooms" for 127.0.0.1 at 2013-01-04 18:17:22 -0800
Processing by ClassroomsController#create as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"Z+rsjO1rY2+P7VdsYd/LwiQr3DZX6r1/Dxh7JGbIOFA=", "name"=>"feed", "commit"=>"Done"}
Completed 406 Not Acceptable in 2ms

I get a blank page with no code (checked the source) in the browser.

perseverance
  • 6,372
  • 12
  • 49
  • 68

1 Answers1

3

Ah, it says "406 Not Acceptable", you are making HTML request from the form but your controller only respond to JS request.

Try adding :remote => true to the form

= form_tag classrooms_path, :html => {:multipart => true}, :remote => true, do |f|

If that doesn't work, you probably haven't include these:

= javascript_include_tag :defaults
= csrf_meta_tag

in your html header (http://stackoverflow.com/questions/4227775/rails-form-for-remote-true-is-not-calling-js-method)

ewiinnnnn
  • 995
  • 7
  • 7
  • What does 406 Not Acceptable represent? Its very vague and not helpful for diagnosing problems.. – perseverance Jan 05 '13 at 02:25
  • thats html error code, This will help you, http://www.checkupdown.com/status/E406.html. Basically by using format.js only, you are saying you only accept JS request, however the request created from the form is HTML by default (unless you use the :remote => true). – ewiinnnnn Jan 05 '13 at 02:27