【问题标题】:Ajax form submit in Ruby on Rails returns 400 Bad RequestRuby on Rails 中的 Ajax 表单提交返回 400 Bad Request
【发布时间】:2015-05-13 09:34:50
【问题描述】:

我有一个要使用 Ajax 提交的表单。现在我收到错误状态 400 Bad Request。

形式为:

<%= form_tag client_contacts_path, role: 'form', id: 'nested_client_contact_form', remote: true, method: 'post' do |f| %>

我在表单提交上有 ajax 调用:

$('#nested_client_contact_form').submit ->
  valuesToSubmit = $(this).serialize()
  $.ajax(
    type: 'POST'
    url: '/client_contacts      ',
    contentType: 'application/json'
    data: valuesToSubmit
    dataType: 'json').success (json) ->
    # some code
    return
  false

这应该会创建新的客户联系人并保留在页面上。没有重定向。

【问题讨论】:

  • 如果使用remote: true,就不需要写那些js代码了。只要确保您的视图文件夹有your_action_name.js 文件。而你的动作确实响应了 js
  • 我已经像在应用程序中一样更改了缩进。

标签: jquery ruby ajax ruby-on-rails-4 coffeescript


【解决方案1】:

最后它只使用 remote: true without CoffeeScript

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-11-10
    • 1970-01-01
    • 2019-06-14
    • 1970-01-01
    • 2013-01-29
    • 2011-09-25
    • 2020-11-01
    • 1970-01-01
    相关资源
    最近更新 更多