简体   繁体   English

Rails AJAX表单提交成功,但是页面上没有任何反应

[英]Rails AJAX form submission succeeds, but nothing happens on page

I'm trying to modify a form in a Rails to-do list app to use AJAX instead of HTML. 我正在尝试在Rails待办事项列表应用程序中修改表单以使用AJAX而不是HTML。 I've gotten the form to submit on the back end, but on the front end, the page does nothing. 我已经将表单提交到后端,但是在前端,该页面不执行任何操作。 Upon submission, I would like to display the list via AJAX. 提交后,我想通过AJAX显示列表。

Here is my form – app/views/lists/_form.html.erb : 这是我的表格– app/views/lists/_form.html.erb

<%= form_for(@list, remote: true) do |f| %>
  <% if @list.errors.any? %>
    <div id="error_explanation">
      <h2><%= pluralize(@list.errors.count, "error") %> prohibited this list from being saved:</h2>

      <ul>
      <% @list.errors.full_messages.each do |message| %>
        <li><%= message %></li>
      <% end %>
      </ul>
    </div>
  <% end %>

  <div class="field">
    <%= f.label :name %><br>
    <%= f.text_field :name %>
  </div>

  <%= f.fields_for :tasks do |task| %>
    <li>
      <%= task.label :name %>       <%= task.text_field :name %>
      <%= task.label :due %>        <%= task.datetime_select :due, ampm: true %>
      <%= task.label :completed %>  <%= task.check_box :completed %>
    </li>
  <% end %>

  <div class="actions">
    <%= f.submit %>
  </div>
<% end %>

And here is the CoffeeScript for the page on which the form is being rendered – app/assets/javascripts/lists.coffee : 这是用于呈现表单的页面的CoffeeScript – app/assets/javascripts/lists.coffee

$(document).ready ->
  $("new_list").on("ajax:success", (e, data, status, xhr) ->
    $("new_list").append xhr.responseText
  ).on "ajax:error", (e, xhr, status, error) ->
    $("new_list").append "<p>Error!</p>"

Submitting the form succeeds on the back end… 提交表单成功在后端...

Started POST "/lists" for ::1 at 2016-06-12 14:28:43 -0400
Processing by ListsController#create as JS
  Parameters: {"utf8"=>"✓", "list"=>{"name"=>"Groceries", "tasks_attributes"=>{"0"=>{"name"=>"Apples", "due(1i)"=>"2016", "due(2i)"=>"6", "due(3i)"=>"12", "due(4i)"=>"18", "due(5i)"=>"28", "completed"=>"0"}, "1"=>{"name"=>"Bananas", "due(1i)"=>"2016", "due(2i)"=>"6", "due(3i)"=>"12", "due(4i)"=>"18", "due(5i)"=>"28", "completed"=>"0"}, "2"=>{"name"=>"Oranges", "due(1i)"=>"2016", "due(2i)"=>"6", "due(3i)"=>"12", "due(4i)"=>"18", "due(5i)"=>"28", "completed"=>"0"}}}, "commit"=>"Create List"}
  User Load (4.7ms)  SELECT  "users".* FROM "users" WHERE "users"."id" = $1  ORDER BY "users"."id" ASC LIMIT 1  [["id", 1]]
   (0.3ms)  BEGIN
  SQL (0.8ms)  INSERT INTO "lists" ("name", "user_id", "created_at", "updated_at") VALUES ($1, $2, $3, $4) RETURNING "id"  [["name", "Groceries"], ["user_id", 1], ["created_at", "2016-06-12 18:28:43.050750"], ["updated_at", "2016-06-12 18:28:43.050750"]]
  SQL (0.7ms)  INSERT INTO "tasks" ("name", "completed", "due", "list_id", "created_at", "updated_at") VALUES ($1, $2, $3, $4, $5, $6) RETURNING "id"  [["name", "Apples"], ["completed", "f"], ["due", "2016-06-12 18:28:00.000000"], ["list_id", 22], ["created_at", "2016-06-12 18:28:43.055160"], ["updated_at", "2016-06-12 18:28:43.055160"]]
  SQL (0.7ms)  INSERT INTO "tasks" ("name", "completed", "due", "list_id", "created_at", "updated_at") VALUES ($1, $2, $3, $4, $5, $6) RETURNING "id"  [["name", "Bananas"], ["completed", "f"], ["due", "2016-06-12 18:28:00.000000"], ["list_id", 22], ["created_at", "2016-06-12 18:28:43.058809"], ["updated_at", "2016-06-12 18:28:43.058809"]]
  SQL (0.4ms)  INSERT INTO "tasks" ("name", "completed", "due", "list_id", "created_at", "updated_at") VALUES ($1, $2, $3, $4, $5, $6) RETURNING "id"  [["name", "Oranges"], ["completed", "f"], ["due", "2016-06-12 18:28:00.000000"], ["list_id", 22], ["created_at", "2016-06-12 18:28:43.061804"], ["updated_at", "2016-06-12 18:28:43.061804"]]
   (4.4ms)  COMMIT
Redirected to http://localhost:3000/lists/22
Completed 302 Found in 42ms (ActiveRecord: 12.1ms)

Started GET "/lists/22" for ::1 at 2016-06-12 14:28:43 -0400
Processing by ListsController#show as JS
  Parameters: {"id"=>"22"}
  List Load (0.6ms)  SELECT  "lists".* FROM "lists" WHERE "lists"."id" = $1 LIMIT 1  [["id", 22]]
  User Load (0.4ms)  SELECT  "users".* FROM "users" WHERE "users"."id" = $1  ORDER BY "users"."id" ASC LIMIT 1  [["id", 1]]
  Task Load (0.9ms)  SELECT "tasks".* FROM "tasks" WHERE "tasks"."list_id" = $1  [["list_id", 22]]
  Rendered lists/show.html.erb within layouts/application (4.9ms)
  Rendered application/_nav.html.erb (0.5ms)
Completed 200 OK in 418ms (Views: 411.3ms | ActiveRecord: 1.8ms)

…but nothing happens on the page. …但是页面上什么都没有发生。 How can I load the newly created list into the page via AJAX? 如何通过AJAX将新创建的列表加载到页面中?

Apologies for the wall of code, and thanks for your help! 不好意思,谢谢您的帮助!

UPDATE: I was able to get the newly created list to render on the page, but it appears below the existing page contents. 更新:我能够将新创建的列表呈现在页面上,但是它显示在现有页面内容的下方 I changed the format.json for saved lists: 我更改了保存列表的format.json

respond_to do |format|
  if @list.save
    format.html { redirect_to @list, notice: 'List was successfully created.' }
    format.json { render json: @list.to_json }
  else
    format.html { render :new }
    format.json { render json: @list.errors, status: :unprocessable_entity }
  end

$("new_list") is looking for a tag <new_list> which most likely doesn't exist. $("new_list")正在寻找一个<new_list>标记,该标记很可能不存在。

Not clear from code shown what it is referencing so if it's an id prefix selector with # and if it's a class prefix with . 从显示的代码中不清楚它在引用什么,因此如果它是带#的id前缀选择器,以及是带的类前缀.

Also not clear how form submit works 也不清楚表单提交的工作方式

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

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