简体   繁体   English

Rails 3.2 JavaScript部分未处理HTML页面

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

On a page there is a form which submit link sends an ajax request to my controller. 在页面上有一个表单,该表单的提交链接将ajax请求发送到我的控制器。 I want the controller to update the resource and then update the resource on the page. 我希望控制器更新资源,然后更新页面上的资源。 The controller responds in the following way: 控制器以以下方式响应:

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

which then uses the following partial: (article/update.js.erb) 然后使用以下部分:(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') 

The ids i am referencing there are present in the calling view within the form 我所引用的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'%>

But clicking the button has no effect. 但是单击该按钮无效。 In the html-inspector i see the payload and the status 200 response. 在html-inspector中,我看到了有效负载和状态200响应。 The logs show: 日志显示:

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)

But inside the view the jquery calls dont have an effect. 但是在视图内部,jQuery调用没有效果。

The response payload is: 响应有效负载为:

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') 
  • The request header "accept" is set to "text/javascript application/json" 请求标头“ accept”设置为“ text / javascript application / json”
  • The response header "content-type" is set to "text/javascript" 响应标头“ content-type”设置为“ text / javascript”

I do not understand why nothing is happening on the page? 我不明白为什么页面上什么都没有发生? Which part am i missing? 我缺少哪一部分? Any suggestions welcome 任何建议欢迎

Change those 改变那些

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

to

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

So it does not render that "true" line, I'm guessing it's messing with your sintax. 因此它不会呈现“ true”行,我猜它与您的sintax混淆了。 And make sure to properly end the lines with ";". 并确保正确地以“;”结尾。

Also, you can add a "debugger;" 另外,您可以添加“调试器”; line before de code so the browser will stop the execution right there and you can debug the response. 在代码之前插入一行,这样浏览器将在此处停止执行,您可以调试响应。

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

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