0
我正在嘗試與Rails 3以及我的應用程序的某些Ajaxification相處。因此,當用戶寫評論時,應該在點擊提交按鈕後通過Ajax插入評論。Rails3 + jQuery:Ajax響應搞砸了
問題是:Ajax響應包含我的application.haml的整個生成的html。我沒有任何線索,如何擺脫這個HTML,只獲得寫在相關的create.js.erb文件中的預期響應。
這裏是我的意見控制器:
class CommentsController < ApplicationController
def new
@comment = Comment.new
end
def create
@article = Article.find(params[:article_id])
@comment = current_user.comments.build(params[:comment])
@comment.article_id = @article.id
respond_to do |format|
if @comment.save
format.html
format.js
else
format.html
format.js
end
end
end
end
的形式(HAML)徵求意見正在尋找這樣的:
#article_write_comment
%h2 Write a comment
#article_write_comment_form
%p Comments are limited to 1000 chars.
= form_for([@article, @article.comments.build], :remote => true) do |f|
.field
= f.text_area :body, :rows => 10, :cols => 50
.actions
= f.submit "Submit"
我create.js.erb文件放置在意見/評論:
$('#article_comments').append('<%= escape_javascript(render(@comment)) %>');
評論partial(views/comments/_comment.haml),應該被追加看起來像這樣:
.comment
= gravatar_for comment.user, :size => 48
%p
%b
= comment.user.name
said:
= time_ago_in_words(comment.created_at)
ago
%p
= simple_format(comment.body)
最後,點擊提交後發送AJAX職位是:
authenticity_token e2rvcKyjPEx8f31U2vemxCGMfVJzxvqTO+03OCAsNkw=
comment[body] Test Test Test
commit Submit
utf8 ✓
但不是這樣的回答:
$('#article_comments').append('<%= escape_javascript(render(@comment)) %>');
我得到這樣的回答與中該網站的HTML代碼: (縮短div的內容,以減少分心)
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<div id='content'>
<div id='header'>
</div>
<div id='container'>
$('#article_comments').append('<div class=\'comment\'>\n <img alt=\"Alex Gieche\" class=\"gravatar\" src=\"http://gravatar.com/avatar/66d7f7aefd1f45ea810cb3f524cc1780?size=48\" />\n <p>\n <b>\n Alex Gieche\n said:\n <\/b>\n less than a minute\n ago\n <\/p>\n <p>\n <p>Test Test Test<\/p>\n <\/p>\n<\/div>\n');
</div>
<div id='sidebar'>
</div>
<div id='footer'>
</div>
</div>
</body>
</html>
註釋未被附加到div(但寫入數據庫)。那麼,我該如何解決這個問題?感謝您的期待!