繁体   English   中英

Rails 3.2 JavaScript部分未处理HTML页面

[英]Rails 3.2 javascript partial is not manipulating the html page

在页面上有一个表单,该表单的提交链接将ajax请求发送到我的控制器。 我希望控制器更新资源,然后更新页面上的资源。 控制器以以下方式响应:

def update
  @article = Article.find(params[:id])
  respond_to do |f| 
    f.js {}
  end 
end

然后使用以下部分:(article / update.js.erb)

<%= Rails.logger.info "in the partial"%>
<%= Rails.logger.info @article.title%>
$("#test").append("<h1> Header</h1>")
$("#json-app").append(" hello <%= @article.title %>");
$("#json-app").append("<p> hello</p>");
$("#comments").append("<%= @article.title %>:");
alert("something")
$("#btn").attr('disabled','disabled') 

我所引用的ID出现在表单的调用视图中

form_for @article ,remote: true, html: {:'data-type' => 'json'}, id: "update_article_form" do |f|
.... 
<div id="comments"> comments </div>
<div id="json-app"> a response would be nice
   <div id="test"> comments</div>
</div> 
.....
<%= f.submit 'Update!', id: "btn", style: 'float: inline-end'%>

但是单击该按钮无效。 在html-inspector中,我看到了有效负载和状态200响应。 日志显示:

Started PUT "/article/2223355" for 127.0.0.1 at 2019-02-18 18:04:29 +0100
Processing by v1::ArticleController#update as JSON
...
in the partial
big pott
Rendered v1/article/update.js.erb (2.9ms)
Completed 200 OK in 41.7ms (Views: 8.2ms | ActiveRecord: 2.4ms)

但是在视图内部,jQuery调用没有效果。

响应有效负载为:

true
true
$("#test").append("<h1> Header</h1>")   
$("#json-app").append(" hello big pott");
$("#json-app").append("<p> hello</p>");
$("#comments").append("big pott ");
alert("something")
$("#btn").attr('disabled','disabled') 
$("input[type=submit]").attr('disabled','disabled') 
  • 请求标头“ accept”设置为“ text / javascript application / json”
  • 响应标头“ content-type”设置为“ text / javascript”

我不明白为什么页面上什么都没有发生? 我缺少哪一部分? 任何建议欢迎

改变那些

<%= Rails.logger.info "in the partial"%>

<%- Rails.logger.info "in the partial" %>

因此它不会呈现“ true”行,我猜它与您的sintax混淆了。 并确保正确地以“;”结尾。

另外,您可以添加“调试器”; 在代码之前插入一行,这样浏览器将在此处停止执行,您可以调试响应。

暂无
暂无

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

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