Question: ActionController::UnknownFormat Error for only format.js

Question

ActionController::UnknownFormat Error for only format.js

Answers 1
Added at 2016-12-28 02:12
Tags
Question

I am simply using -

respond_to do |format|
    format.js
end

in my create action. Don't know why but i am getting an the UnknownFormat error. The error is gone if i put in both "format.html" and "format.js". But i want to stay in the same page and make an ajax call. I have gone through like 5 pages of google search results for every possible search and still nothing works for me. Can anyone help me out here?

My form is like this -

<%= form_for [:home, Photo.new], remote: true, :html => {:id => "new-photo-form"} do |f| %>

    <div id="upload-field">
        <%= f.file_field :image %>
    </div>

    <%= f.hidden_field :album_id, :value => album.id %>

    <div id="photo-add-link">
        <%= link_to 'Add Selected Images', '#', remote: true, :onclick => "$('#new-photo-form').submit()" %>
    </div>

<% end %>

server logs -

Started POST "/home/photos" for 127.0.0.1 at 2016-12-28 08:23:38 +0530
Processing by Home::PhotosController#create as HTML
Parameters: {"utf8"=>"✓", "photo"=>{"image"=>#  <ActionDispatch::Http::UploadedFile:0x007fd5541a0578 @tempfile=#  <Tempfile:/tmp/RackMultipart20161228-6069-175ahz0.jpg>,   @original_filename="emilia-clark.jpg", @content_type="image/jpeg",   @headers="Content-Disposition: form-data; name=\"photo[image]\";   filename=\"emilia-clark.jpg\"\r\nContent-Type: image/jpeg\r\n">,   "album_id"=>"68"}}
User Load (0.1ms)  SELECT  "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1  [["id", 1]]
Completed 406 Not Acceptable in 2ms (ActiveRecord: 0.1ms)

ActionController::UnknownFormat (ActionController::UnknownFormat):
app/controllers/home/photos_controller.rb:11:in `create'


Rendered /home/santhosh/.rvm/gems/ruby-2.3.0/gems/actionpack-4.2.6/lib/action_dispatch/middleware/templates/rescues/_source.erb (3.4ms)
Rendered /home/santhosh/.rvm/gems/ruby-2.3.0/gems/actionpack-4.2.6/lib/action_dispatch/middleware/templates/rescues/_trace.html.erb (1.7ms)
Rendered /home/santhosh/.rvm/gems/ruby-2.3.0/gems/actionpack-4.2.6/lib/action_dispatch/middleware/templates/rescues/_request_and_response.html.erb (0.8ms)
Rendered /home/santhosh/.rvm/gems/ruby-2.3.0/gems/actionpack-  4.2.6/lib/action_dispatch/middleware/templates/rescues/diagnostics.html.erb within rescues/layout (16.2ms)
Rendered /home/santhosh/.rvm/gems/ruby-2.3.0/gems/web-console-2.3.0/lib/web_console/templates/_markup.html.erb (0.3ms)
Rendered /home/santhosh/.rvm/gems/ruby-2.3.0/gems/web-console-2.3.0/lib/web_console/templates/_inner_console_markup.html.erb within layouts/inlined_string (0.2ms)
Rendered /home/santhosh/.rvm/gems/ruby-2.3.0/gems/web-console-2.3.0/lib/web_console/templates/_prompt_box_markup.html.erb within layouts/inlined_string (0.4ms)
Rendered /home/santhosh/.rvm/gems/ruby-2.3.0/gems/web-console-2.3.0/lib/web_console/templates/style.css.erb within layouts/inlined_string (1.2ms)
Rendered /home/santhosh/.rvm/gems/ruby-2.3.0/gems/web-console-2.3.0/lib/web_console/templates/console.js.erb within layouts/javascript (16.2ms)
Rendered /home/santhosh/.rvm/gems/ruby-2.3.0/gems/web-console-2.3.0/lib/web_console/templates/main.js.erb within layouts/javascript (0.2ms)
Rendered /home/santhosh/.rvm/gems/ruby-2.3.0/gems/web-console-2.3.0/lib/web_console/templates/error_page.js.erb within layouts/javascript (0.4ms)
Rendered /home/santhosh/.rvm/gems/ruby-2.3.0/gems/web-console-  2.3.0/lib/web_console/templates/index.html.erb (29.5ms)
Answers to

ActionController::UnknownFormat Error for only format.js

nr: #1 dodano: 2016-12-28 04:12

I noticed that you are using a file_field in your form. JavaScript doesn't have access to your filesystem, so you can't create an ajax request to upload files. There are some gems to workaround this. check this gem.

Source Show
◀ Wstecz