【发布时间】:2019-07-12 03:20:17
【问题描述】:
在页面上有一个提交链接的表单向我的控制器发送 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”
我不明白为什么页面上什么也没有发生?我错过了哪一部分?欢迎任何建议
【问题讨论】:
标签: javascript jquery ruby-on-rails ajax ruby-on-rails-3