【问题标题】:AJAX/Javascript Error Ruby on RailsAJAX/Javascript 错误 Ruby on Rails
【发布时间】:2009-11-19 18:50:43
【问题描述】:

好的,这就是我所在的位置。我在这里有三个不同的东西,我非常接近让它发挥作用。

我有一个复选框 = 值为 1 的表单,它为“帖子”收集“投票”并将其发送到带有 post_id 的数据库。这很好用。

我有一个部分模板,可以在表单下方显示特定帖子的投票。

我设置了 AJAX,以便在提交投票时将其显示到 @post.votes。

数据库正在接收投票,部分正在显示它们,但是当 javascripts 运行时出现以下错误:

RJS error:

TypeError: Result of expression '((position == 'before' || position == 'after')
        ? element.parentNode : element)' [null] is not an object.

然后:

Element.insert("votes", { top: "<div class=\"vote\" id=\"vote_44\">\n\n\n\t\t1\n\n\n\n\t\n\t\n\t</div>\n\t\n" });
$("vote_44").visualEffect("highlight");
$("vote").reset();

这是截图。alt text http://bwgblog.com/screen.png

这是其余的代码供参考。顺便说一句,我对所有编程都很陌生,如果这看起来很简单,我很抱歉。

votes_controller.rb

class VotesController < ApplicationController

  def create
    @post = Post.find(params[:post_id])
    @vote = @post.votes.create!(params[:vote])

    respond_to do |format|
       format.html { redirect_to @post}
       format.js
     end
  end
end

远程表单和部分位于 /posts/show.html.erb。你会看到我在顶部设置的 cmets 部件,一切都很好。有一些 div,因为这部分已经设置了样式。我的预感是错误就在这个页面的某个地方。

<div id="backto"<%= link_to 'Back to all BattleCries', posts_path %></div>
<%= render :partial => @post %><br/>


<p5>Add a Comment</p5>
<div id="belt">
    <div id="belttext">
<% remote_form_for [@post, Comment.new] do |f| %>
    <p>
        <%= f.text_area ( :body, :class => "commentarea") %>
    </p>
    <%= f.submit "Add Comment"%>
<% end %>
</div>
<div id="beltbottom">
</div>
</div><br/>
<br/><p5>Comment Stream </p5> 
<div id="comments">
    <%= render :partial => @post.comments %>

</div>

<p>
<% remote_form_for [@post, Vote.new] do |f| %>
    <p>
        <%= f.label :vote %>
        <%= f.check_box :vote %>
    </p>
    <%= f.submit "Vote" %>
<% end %>

    <%= render :partial => @post.votes %>

</p>

这是 /votes/_vote.html.erb 中的部分投票:

<% div_for vote do %>
        <%= h(vote.vote) %> 
    <% end %>

这里是 /votes/create.js.rjs 文件:

page.insert_html :top, :votes, :partial => @vote
page[@vote].visual_effect :highlight
page[:vote].reset

最后这里是我的 /layouts/posts.html.erb 文件,我不认为错误在这里,因为 cmets AJAX 与这个包装器配合得很好。

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
       "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
  <meta http-equiv="content-type" content="text/html;charset=UTF-8" />
  <title>Posts: <%= controller.action_name %></title>
  <%= stylesheet_link_tag 'scaffold' %>
  <% auto_discovery_link_tag :atom, formatted_posts_path(:atom) %>
  <%= javascript_include_tag :all %>
<script type="text/javascript">
function toggleLayer( whichLayer )
{
  var elem, vis;
  if( document.getElementById ) // this is the way the standards work
    elem = document.getElementById( whichLayer );
  else if( document.all ) // this is the way old msie versions work
      elem = document.all[whichLayer];
  else if( document.layers ) // this is the way nn4 works
    elem = document.layers[whichLayer];
  vis = elem.style;
  // if the style.display value is blank we try to figure it out here
  if(vis.display==''&&elem.offsetWidth!=undefined&&elem.offsetHeight!=undefined)
    vis.display = (elem.offsetWidth!=0&&elem.offsetHeight!=0)?'block':'none';
  vis.display = (vis.display==''||vis.display=='block')?'none':'block';
}
</script>
</head>
<body>
    <div id="stage">
        <div id="leftbar">
        </div>
        <div id="middlebar">
            <div id="topmiddle">
            </div>
            <div id="middlecontent">
                <div id="middlecontenttext">
                    <p style="color: green"><%= flash[:notice] %></p>
                    <%= yield %>

                </div>
            </div>

        </div>
        <div id="rightbar">
            <div id="rightbarband">
                <p5>Most<br/>Commented<br/>Battlecries</p5>
                <p>This is where these will go.</p><br/><br/>
                <p5>Search BattleCries</p5>
                <input = "submit"><br/>
                <br/><br/>          
                <p5>Get the Wristband</p5>
                <img src="../images/wristband.png" width="208" height="79" alt="Wristband">
                <p>Tell us the title of your Life BattleCry and we will print it on a reminder wristband. Click here and be done in 60 seconds.</p>
                <br/><br/>
                <p5>Get the <br/>T-Shirt</p5>
                <img src="../images/logoshirt.png" width="208" height="42" alt="Logoshirt">
                <p>this is where an image and also some very interesting text about the sweet wristband will go</p>
                <br/><br/>

                <p></p>
            </div>

        </div>

    </div>
</body>
</html>

【问题讨论】:

  • 你能确定错误消息中sn-p引用的代码在哪里吗(“((位置=='之前'||位置=='之后')?element.parentNode:元素)”)出现?它似乎不在您在此处发布的任何示例中。
  • 对不起,我认为它出现在浏览器的弹出窗口中。如果可以的话,我会用屏幕截图更新问题。

标签: javascript ruby-on-rails ruby ajax


【解决方案1】:

在 RJS 模板 (/votes/create.js.rjs) 中有一行:

page.insert_html :top, :votes, :partial => @vote

我认为您可能需要在 /posts/show.html.erb 中包含 id='votes' 的 DIV,以便 javascript 知道在哪里插入此 HTML。

insert_html 的文档是 here

【讨论】:

  • 添加
    从功能的角度来看是可行的,但我现在收到此错误。相同的浏览器显示错误:TypeError: 表达式'$("vote")' [null] 的结果不是对象。 Element.insert("votes", { top: "
    \n\t\t0\t\n\t
    \n\t\ n" }); $("vote_50").visualEffect("highlight"); $("投票").reset();有什么想法吗?
  • 这是两个单独的错误,只是没有在 cmets 中格式化,第一个以 ...object 结尾。
  • 刚刚修好了。摆脱了 create.js.rjs 中的 page[:vote].reset 并且它可以工作。很奇怪。
  • 很高兴你把它整理好了。显示错误的警告框是由 rails 引起的 - 这就是在开发模式下运行时显示 RJS 错误的方式。在 config/development.rb 中,您将看到“config.action_view.debug_rjs = true”行。这就是控制这些错误显示的原因。您的 RJS 将在生产模式下静默失败。不管怎样,很高兴听到你想通了。
猜你喜欢
相关资源
最近更新 更多
热门标签