【问题标题】:Can't get if else to work right in HAML javascript无法在 HAML javascript 中正常工作
【发布时间】: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


【解决方案1】:

也许这会起作用(我现在无法自己检查)

- if @comment.errors.any?
  :plain
    $("#CommentError").html("#{escape_javascript(render(:partial => "error"))}");
    $("#CommentError").attr("visible", "true");
- else
  :plain
    $("#CommentsTable").append("#{escape_javascript(render(@comment))}");
    $("#CommentError").attr("visible", "false");

【讨论】:

  • 不起作用。通过查看它发送的 broswer 开发工具网络数据:if @comment.errors.any? $("#CommentError").html("<h2>\n 1 error\n prohibited this comment from being saved\n<\/h2>\n<ul>\n <li>\n Commenter is too short (minimum is 5 characters)\n <\/li>\n<\/ul>\n"); $("#CommentError").attr("visible", "true"); else ...etc
  • 哦,我的错 - 我错过了 - 说 HAML 这是 ruby​​ 代码。尝试更新的变体
  • 谢谢,它成功了,只需要修复缩进并将:plain 过滤器移到- if 和- else 内
  • 把这一切都放在%script{type: "text/javascript"} 中,对吧?
【解决方案2】:

我认为您在这里混合了 Ruby 和 Javascript 有点过多。试试这个吧。

if @comment.errors.any?
  :javascript
    $("#CommentError").html("#{escape_javascript(render(:partial => "error"))}");
    $("#CommentError").attr("visible", "true");
else
  :javascript
    $("#CommentsTable").append("#{escape_javascript(render(@comment))}");
    $("#CommentError").attr("visible", "false");

更多文档:http://haml.info/docs/yardoc/file.REFERENCE.html#javascript-filter

【讨论】:

  • 不。起初出现缩进错误,然后取消缩进 :javascript 过滤器及其下的内容,然后它只生成部分类并发送:if @comment.errors.any? <script> $("#CommentError").html("<h2>\n 1 error\n prohibited this comment from being saved\n<\/h2>\n<ul>\n <li>\n Commenter is too short (minimum is 5 characters)\n <\/li>\n<\/ul>\n"); $("#CommentError").attr("visible", "true"); </script> <script> $("#CommentsTable").append("<div class=\'comment\' 等...
猜你喜欢
  • 2013-05-13
  • 1970-01-01
  • 2015-05-23
  • 2012-11-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-27
  • 1970-01-01
相关资源
最近更新 更多