我正在工作一個rails 4項目,我一直在使用軌道Ajax方法將數據從控制器傳輸到js.erb,這一直工作正常。我注意到有一段時間,所有js.erb javascript已停止工作,但從服務器查看該文件的請求發送爲js
,並且它呈現正確。所有js.erb運行但沒有在rails 4執行
一個的答覆:
Started GET "/comments/new?comment%5Bcommentable_id%5D=26&comment%5Bcommentable_type%5D=Question" for 127.0.0.1 at 2016-08-21 13:11:32 +0100
Processing by CommentsController#new as JS
Parameters: {"comment"=>{"commentable_id"=>"26", "commentable_type"=>"Question"}}
User Load (0.5ms) SELECT "users".* FROM "users" WHERE "users"."id" = 20 ORDER BY "users"."id" ASC LIMIT 1
(0.2ms) BEGIN
SQL (0.3ms) UPDATE "users" SET "last_requested_at" = $1, "updated_at" = $2 WHERE "users"."id" = 20 [["last_requested_at", Sun, 21 Aug 2016 12:11:32 UTC +00:00], ["updated_at", Sun, 21 Aug 2016 12:11:32 UTC +00:00]]
(1.3ms) COMMIT
User Load (1.0ms) Select users.* from users, user_categories where user_categories.user_id = users.id and user_categories.category_id in (select user_categories.category_id from user_categories where user_categories.user_id = 20) and users.id not in (20) and users.id not in (SELECT users.id FROM users INNER JOIN relationships ON users.id = relationships.followed_id and relationships.follower_id = 20) group by users.id limit 5
Rendered comments/_form.html.erb (0.8ms)
Rendered comments/new.js.erb (2.0ms)
Completed 200 OK in 12ms (Views: 3.6ms | ActiveRecord: 3.3ms)
這happends中的所有應用程序js.erb文件,但所有的資產腳本(vanilar,咖啡和jQuery)文件的作品找到。我現在一直在調試幾個月,我不知道什麼是錯的。
當我試圖檢查表單鉻我得到200響應作爲js但沒有執行,我也嘗試了不同的瀏覽器。
這裏是new.js.erb
<% if @comment.commentable_type == "Question" %>
$('#comment-form').append('<%= escape_javascript render("form") %>');
$('.comment_link').hide();
<% else %>
$('#comment-form_<%= @comment.commentable_id %>').append('<%= escape_javascript render("form") %>');
$('.comment_link').hide();
<% end %>
$('.cancel-comment').click(function() {
$('#new_comment, .comment-form').hide();
$('.comment_link').show();
})
這裏的新的評論是,打電話的觸發阿賈克斯再審的鏈接:
<%= link_to " Add a comment", new_comment_path(:comment => { :commentable_type => @question.class.name,
:commentable_id => @question.id}),
:remote => true,
:class => "comment_link",
id:"comment_link" %>
控制器的方法:
def new
@comment = Comment.new(comment_params)
respond_to do |format|
format.js { render layout: false }
end
end
以前的工作?您是否將擴展名添加到您的視圖文件.js中。當以獨立方式打開時,文件內容是什麼? –
你選擇了適當的'DOM'元素來追加數據嗎? – uzaif
@JamesTan是的,他們都在正常工作。這現在在所有其他.js.erb文件中都沒有,即使js警報在那裏工作。 uzaif是的,我選擇Dom非常好,旁邊的各種JavaScript方法沒有執行。 –