ActionController :: UnknownFormat只有format.js的错误

时间:2016-12-28 01:56:30

标签: ruby-on-rails ruby-on-rails-3 ruby-on-rails-4

我只是在使用 -

respond_to do |format|
    format.js
end

在我的创建动作中。不知道为什么,但我得到一个UnknownFormat错误。如果我同时放入" format.html"那么错误就消失了。和" format.js"。但我想留在同一页面并进行ajax调用。对于每个可能的搜索,我已经浏览了5页google搜索结果,但对我来说仍然无效。有人可以帮我从这里出去吗?

我的表格是这样的 -

<%= 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 %>

服务器日志 -

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)

1 个答案:

答案 0 :(得分:0)

我注意到您在表单中使用了file_field。 JavaScript无权访问您的文件系统,因此您无法创建上传文件的ajax请求。有一些宝石可以解决这个问题。检查this gem。