简体   繁体   English

Rails在AJAX上的未知格式

[英]Rails unknown format on AJAX

I'm trying to implement a very simple file form using the remotipart gem. 我正在尝试使用remotipart gem实现一个非常简单的文件格式。 Most of my files are the exact same as the tutorial ones: 我的大多数文件与教程文件完全相同:

timeline.html.erb : timeline.html.erb:

<%= form_tag new_feed_path(:format => "js"), remote: true, :html => { :multipart => true } do |f| %>
  <%= hidden_field_tag :brief_id, @brief.id %>
  <%= file_field_tag :file %>
  <%= submit_tag "Send", class: "btn btn-success" %>
<% end %>

briefs_controller.rb briefs_controller.rb

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

new_feed.js.erb new_feed.js.erb

alert('success!');

<% if remotipart_submitted? %>
  alert('submitted via remotipart')
<% else %>
  alert('submitted via native jquery-ujs')
<% end %>

But everytime I submit the form, I get the following error in the logs: 但是,每次提交表单时,日志中都会出现以下错误:

Processing by ResourcesController#create as HTML
Completed 406 Not Acceptable in 14ms
ActionController::UnknownFormat - ActionController::UnknownFormat:

Did I miss something ? 我错过了什么 ? I know ajax file upload can be tricky on RoR, but remotipart seems to be a viable solution. 我知道在RoR上上传Ajax文件可能很棘手,但是remotipart似乎是一个可行的解决方案。

EDIT I managed to fix the first issue by adding :format => "js" , but now I face another problem: none of the form datas are sent. 编辑我设法通过添加:format => "js"来解决第一个问题,但是现在我遇到了另一个问题:没有发送任何表单数据。 In fact, here are the sent params: 实际上,这是发送的参数:

{"controller"=>"briefs", "action"=>"new_feed"}

Check out the example from Remotipart's docs . 查看Remotipart的docs中的示例。

Looks like you're not passing :html => { :multipart => true } to form_for 看来您没有将:html => { :multipart => true }传递给form_for

try it 试试吧

<%= form_tag new_feed_path, html: {multipart: true }, method: :post, remote:true do |f| %>
  <%= hidden_field_tag :brief_id, @brief.id %>
  <%= file_field_tag :file %>
  <%= submit_tag "Send", class: "btn btn-success" %>
<% end %>

edit 编辑

try it 试试吧

install this gem pry 安装这个宝石pry

RailsCast Pry RailsCast撬

briefs_controller.rb briefs_controller.rb

def new_feed
  binding.pry #just to be sure that this action is not called 
  puts params
  respond_to do |format|
    format.js { render 'new_feed') # modify this
  end
end

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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