簡體   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