模态隐藏在使用ajax的rails 3.2.21上的ruby中不起作用

时间:2016-02-18 10:34:59

标签: javascript ruby-on-rails ruby ajax

我想通过ajax提交表单。渲染new.js.erb后,虽然正在调用create controller,但它不会创建create.js.erb。以某种方式跳过create.js.erb中的所有代码。因此,我的模态既不是关闭也不是渲染任何页面。

这是我的模态文件:

<div class="modal-dialog">  
  <div class="modal-content">

<h3> New Article </h3>

<%= form_for @article, remote: true , html: {style: "display:inline;"} do |f| %>
  <div class ="modal-body">
  <div class="field form-group">
  <ul class="errors"></ul>

    <%= f.label :title %><br>
    <%= f.text_field :title ,class: 'form-control'%>
  </div>
 
  <div class="field form-group">
    <%= f.label :text %><br>
    <%= f.text_field :text ,class: 'form-control' %>
  </div>
  </div>
 
<div class="modal-footer">
  
    <%= f.submit class: "btn btn-primary"%>
    <%= link_to "Cancel", "#", class: "btn", data: {dismiss: "modal"} %>

</div>
<% end %>

</div>
</div>

档案new.js.erb:

$("#article-modal").html("<%= escape_javascript(render 'new') %>")
$("#article-modal").modal("show")

文件create.js.erb

<%= render "save" %>

文件save.js.erb:

$("ul.errors").html("")
<% if @article.errors.any? %>
	<% @article.errors.full_messages.each do |message| %>
	$("ul.errors").append($("<li />").html("<%= message.html_safe %>"))
	<% end %>
<% else %>
	$(".article-index").html("<%= escape_javascript(render 'index')%>")
	$("#article-modal").modal("hide")
<% end %>

我是Ruby的新手。请帮助。

这将显示在控制台中:

Started GET "/articles/new" for 127.0.0.1 at 2016-02-18 17:13:48 +0530
Processing by ArticlesController#new as JS
  User Load (0.2ms)  SELECT "users".* FROM "users" WHERE "users"."id" = 1 LIMIT 1
  Rendered articles/_newedit.html.erb (9.5ms)
  Rendered articles/_new.html.erb (10.6ms)
  Rendered articles/new.js.erb (11.8ms)
Completed 200 OK in 17.0ms (Views: 14.8ms | ActiveRecord: 0.2ms | Solr: 0.0ms)


Started POST "/articles" for 127.0.0.1 at 2016-02-18 17:13:58 +0530
Processing by ArticlesController#create as JS
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"NfHEpBGzNdP0DBxhKpw4QH5QGK+dheQAwVDpCxruZVw=", "article"=>{"title"=>"Rohit", "text"=>"asdf"}, "commit"=>"Create Article"}
  User Load (0.2ms)  SELECT "users".* FROM "users" WHERE "users"."id" = 1 LIMIT 1
  Article Load (0.1ms)  SELECT "articles".* FROM "articles" 
   (0.0ms)  begin transaction
  SQL (2.2ms)  INSERT INTO "articles" ("created_at", "text", "title", "updated_at") VALUES (?, ?, ?, ?)  [["created_at", Thu, 18 Feb 2016 11:43:58 UTC +00:00], ["text", "asdf"], ["title", "Rohit"], ["updated_at", Thu, 18 Feb 2016 11:43:58 UTC +00:00]]
  SOLR Request (12.3ms)  [ path=#<RSolr::Client:0x007f89fc963808> parameters={data: <?xml version="1.0" encoding="UTF-8"?><add><doc><field name="id">Article 60</field><field name="type">Article</field><field name="type">ActiveRecord::Base</field><field name="class_name">Article</field><field name="text_text">asdf</field><field name="title_text">Rohit</field></doc></add>, headers: {"Content-Type"=>"text/xml"}, method: post, params: {:wt=>:ruby}, query: wt=ruby, path: update, uri: http://localhost:8982/solr/update?wt=ruby, open_timeout: , read_timeout: , retry_503: , retry_after_limit: } ]
   (90.6ms)  commit transaction
Redirected to http://localhost:3000/articles/60
  SOLR Request (259.9ms)  [ path=#<RSolr::Client:0x007f89fc963808> parameters={data: <?xml version="1.0" encoding="UTF-8"?><commit/>, headers: {"Content-Type"=>"text/xml"}, method: post, params: {:wt=>:ruby}, query: wt=ruby, path: update, uri: http://localhost:8982/solr/update?wt=ruby, open_timeout: , read_timeout: , retry_503: , retry_after_limit: } ]
Completed 302 Found in 406.3ms (ActiveRecord: 93.1ms)


Started GET "/articles/60" for 127.0.0.1 at 2016-02-18 17:13:58 +0530
Processing by ArticlesController#show as JS
  Parameters: {"id"=>"60"}
  User Load (0.2ms)  SELECT "users".* FROM "users" WHERE "users"."id" = 1 LIMIT 1
  Article Load (0.1ms)  SELECT "articles".* FROM "articles" WHERE "articles"."id" = ? LIMIT 1  [["id", "60"]]
  Rendered articles/show.html.erb within layouts/application (0.4ms)
Completed 200 OK in 35.1ms (Views: 32.6ms | ActiveRecord: 0.3ms | Solr: 272.2ms)

0 个答案:

没有答案