【发布时间】:2013-11-10 13:42:28
【问题描述】:
用 cmets 做一个小演示迷你博客应用程序。使用 ROR 和 HAML。我希望用 AJAX 创建 cmets,所以我编写了 create.js.haml。如果在创建过程中出现任何错误,我也希望出现错误。
create.js.haml
:plain
if #{@comment.errors.any?} {
$("#CommentError").html("#{escape_javascript(render(:partial => "error"))}");
$("#CommentError").attr("style", "display: inline");
} else {
$("#CommentsTable").append("#{escape_javascript(render(@comment))}");
$("#CommentError").attr("style", "display: false");}
这不起作用。条件计算结果为true 或false,但代码未执行。但是,如果我将条件的任何部分放在它自己的 create.js.haml 中,它就会起作用。
这在出现错误的情况下有效:
:plain
$("#CommentError").html("#{escape_javascript(render(:partial => "error"))}");
$("#CommentError").attr("style", "display: inline");
这在没有错误的情况下有效,我需要添加评论:
:plain
$("#CommentsTable").append("#{escape_javascript(render(@comment))}");
$("#CommentError").attr("style", "display: false");
这是视图,虽然我认为问题不存在:
%p
%strong Title:
= @post.title
%p
%strong Text:
= @post.text
%h3 Comments:
.CommentsArea
= render @post.comments
#CommentError{:style => 'display: none'}
%h2 Add a comment:
= render "comments/form"
= link_to 'Back', posts_path
|
= link_to 'Edit', edit_post_path(@post)
【问题讨论】:
-
Ruby、javascript 和 HAML?对于一个 JS 文件来说,混音实在是太多了……你测试过 create.js.erb 并且它在那里工作吗?
-
你可以用 erb 做的任何事情都可以用 HAML 做,反之亦然。这不是生产代码,只是一个练习,试图学习这些东西。
标签: javascript jquery ruby-on-rails ajax haml