简体   繁体   English

Rails,输入模糊后自动提交表单返回:ActionController :: UnknownFormat

[英]Rails, auto-submit form after input blur returns: ActionController::UnknownFormat

The Goal: Auto submit the form after blur from any input. 目标: 在任何输入模糊之后自动提交表单。

My approach: 我的方法:

Using remote: true in the form, to tell Rails I want to submit via Ajax, then in controller#create, I added a respond_to block and finally, I also created the create.js.erb file to complete. 在表单中使用remote: true来告诉Rails我要通过Ajax提交,然后在controller#create中,添加了respond_to块,最后,我还创建了create.js.erb文件来完成。

And that's working just fine if someone clicks 'submit' ... but what about auto-submit when blur ? 如果有人单击“提交”,那很好用...但是blur时自动提交呢? To achieve that, I followed this SO and added an autoSubmitForm() function, but didn't work, showing this error: 为此,我遵循了此SO并添加了autoSubmitForm()函数,但没有起作用,显示了此错误:

Now, the error: 现在,错误:

Started POST "/e/0-8d5ac093-3611-4b46-a220-41eb880e6732/elements/10/posts" for 127.0.0.1 at 2018-03-08 15:41:38 -0600
  User Load (0.4ms)  SELECT  "users".* FROM "users" WHERE "users"."id" = $1 ORDER BY "users"."id" ASC LIMIT $2  [["id", 2], ["LIMIT", 1]]
Processing by PostsController#create as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"MZQoS9PsHn50pAXmPUnbk2NjzgLOSps0rn87hYw/lyXD96N0EyzRXTVChazenb1AxL9GnpZ/GH7do3LsEp9zUg==", "answer"=>{"message"=>"MTY"}, "profile_id"=>"0-8d5ac093-3611-4b46-a220-41eb880e6732", "element_id"=>"10"}
  Profile Load (0.2ms)  SELECT  "profiles".* FROM "profiles" WHERE "profiles"."profileable_id" = $1 AND "profiles"."profileable_type" = $2 LIMIT $3  [["profileable_id", 2], ["profileable_type", "User"], ["LIMIT", 1]]
   (0.1ms)  BEGIN
  Element Load (0.2ms)  SELECT  "elements".* FROM "elements" WHERE "elements"."id" = $1 LIMIT $2  [["id", 10], ["LIMIT", 1]]
  SQL (0.3ms)  INSERT INTO "posts" ("element_id", "message", "created_at", "updated_at", "profile_id") VALUES ($1, $2, $3, $4, $5) RETURNING "id"  [["element_id", 10], ["message", "MTY"], ["created_at", "2018-03-08 21:41:38.121299"], ["updated_at", "2018-03-08 21:41:38.121299"], ["profile_id", 4]]
   (6.0ms)  COMMIT
Completed 401 Unauthorized in 12ms (ActiveRecord: 6.8ms)



ActionController::UnknownFormat - ActionController::UnknownFormat:
  app/controllers/posts_controller.rb:9:in `create'

Started POST "/__better_errors/f572adc255d87208/variables" for 127.0.0.1 at 2018-03-08 15:41:38 -0600


ActionController::UnknownFormat - ActionController::UnknownFormat:
  app/controllers/answers_controller.rb:9:in `create'

Started POST "/__better_errors/f572adc255d87208/variables" for 127.0.0.1 at 2018-03-08 15:41:38 -0600

I also tried adding onchange: 'this.form.submit();' 我也尝试添加onchange: 'this.form.submit();' to input, but it returns the same error. 输入,但返回相同的错误。

The post is created ok, but it breaks when implementing respond_to . 帖子创建的很好,但是在实现respond_to时会中断。

auto_submit_function.js auto_submit_function.js

$(document).on('turbolinks:load', function() {
  autoSubmitForm()
})

function autoSubmitForm() {
  $('form :input').blur(function() {
    $(this).closest('form').submit();
  });
}

controller 控制者

class PostsController < ApplicationController
  before_action :authenticate_user! # I'm using devise

  def create
    @answer = Post.new(posts_params)
    @answer.save
    respond_to do |format|
      format.js
    end
  end

  private

  def posts_params
    params.permit(:post).require(:message)
  end
end

_form.html.haml _form.html.haml

= form_for [article, post], 
  remote: true, 
  authenticity_token: true, 
  html: { id: "new_post_#{article.id}" } do |form|

  = form.text_field :message
  = form.label :message
  = form.submit

create.js.erb create.js.erb

console.log('I can take it from here, thanks!')

I just found a slightly different approach. 我只是发现了一种稍微不同的方法。

Instead of: 代替:

$(document).on('turbolinks:load', function() {
  autoSubmitForm()
})

function autoSubmitForm() {
  $('form :input').blur(function() {
    $(this).closest('form').submit(); // <<<<< Remove this
    $('#submit_' + $(this).attr('id')).trigger('click'); // <<< Add this
  });
}

And add a custom id for each submit button (I have multiple forms in the same view). 并为每个submit按钮添加一个自定义ID(我在同一视图中有多个表单)。 Also, I hide with css the submit button. 另外,我用CSS隐藏了Submit按钮。

Basically, instead of auto submit I'm auto clicking on submit. 基本上,不是auto submit ,而是auto clicking提交。

Even when this works, It feels like there should be a better solution for this scenario. 即使这种方法可行,也感觉应该为这种情况提供更好的解决方案。

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

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