【问题标题】:What's wrong with my Ruby on Rails code?我的 Ruby on Rails 代码有什么问题?
【发布时间】:2011-03-03 22:30:31
【问题描述】:

所以我一直在尝试使用 rjs 向我的主页添加一些 AJAX,但我在弹出窗口中收到了这个 javascript 错误:

RJS error:

TypeError: Cannot call method 'getElementsByTagName' of null

以下是所有相关代码。

这是我在 app/views/microposts/create.rjs 中的代码:

page.insert_html :bottom, :feed_items, :partial => 'shared/feed_item', :object => @micropost
page.replace_html :user_info, pluralize(current_user.microposts.count, "micropost")
page[:micropost_form].reset
page.replace_html :notice, flash[:notice]
flash.discard

这里是 app/views/pages/home.rb:

<% if signed_in? %>
  <table class="front" summary="For signed-in users">
    <tr>
      <td class="main">
      <h1 class="micropost">What's happening?</h1>
    <%= render 'shared/micropost_form' %>
    <%= render 'shared/feed' %>
  </td>
  <td class="sidebar round">
    <%= render 'shared/user_info' %>
    <%= render 'shared/stats' %>
  </td>
 </tr>
</table>
<% else %>
<h1>Sample App</h1>

<p>
  This is the home page for the
  <a href="http://railstutorial.org/">Ruby on Rails Tutorial</a>
  sample application.
</p>

<%= link_to "Sign up now!", signup_path, :class => "signup_button round" %>
<% end %>

这是我的提要部分 app/views/shared/_feed.html.erb:

<% unless @feed_items.empty? %>
  <table id="feed_items" class="microposts" summary="User microposts">
    <%= render :partial => 'shared/feed_item', :collection => @feed_items %>
  </table>
  <%= will_paginate @feed_items %>
<% end %>

这是我的提要项目部分 app/views/shared/_feed_item.html.erb

 <tr id = "feed_item">
  <td class="gravatar">
    <%= link_to gravatar_for(feed_item.user), feed_item.user %>
  </td>
 <td class="micropost">
    <span class="user">
      <%= link_to feed_item.user.name, feed_item.user %>
    </span>
    <span class="content"><%= feed_item.content %></span>
    <span class="timestamp">
      Posted <%= time_ago_in_words(feed_item.created_at) %> ago.
    </span>
  </td>
  <% if current_user?(feed_item.user) %>
  <td>
    <%= link_to "delete", feed_item, :method => :delete,
                                     :confirm => "You sure?",
                                     :title => feed_item.content %>
  </td>
  <% end %>
</tr>

最后是我的微博控制器的相关部分:

class MicropostsController < ApplicationController
  before_filter :authenticate, :only => [:create, :destroy]
  before_filter :authorized_user, :only => :destroy

  def create
    @micropost  = current_user.microposts.build(params[:micropost])
    respond_to do |format|
      if @micropost.save
        flash[:success] = "Micropost created!"
        format.html { redirect_to root_path }
        format.js   
      else
        @feed_items = []
        render 'pages/home'
      end  
    end
  end

【问题讨论】:

  • 请在标题中描述您的问题,标题只是通用的,可能是我收到 TypeError: Cannot call method 'getElementsByTagName' of null in Rails。

标签: javascript ruby-on-rails ruby ajax rjs


【解决方案1】:

鉴于这是一个 RJS 错误,问题出在create.rjs

尝试注释掉 page.replace_html :notice, flash[:notice] 看看是否有效。

【讨论】:

  • 啊,好吧。现在我没有 js 错误,但是直到我刷新页面后新帖子才会出现。我也不再收到 Flash 消息...
  • 哦,没关系,帖子确实出现了!但是,我如何获得 Flash 消息?为什么page.replace_html :notice, flash[:notice] 不起作用?
  • 它不工作的原因是因为在您的 HTML 中(可能)没有带有 idnotice 的元素。尝试将&lt;div id="notice"&gt;&lt;/div&gt; 添加到您的页面某处,取消注释该行并查看它是否有效。
  • 我尝试将它添加到我的应用程序布局中正在呈现 flash 的位置,但无法通过 id 获取它。
  • 我添加了一个关于这个的问题:stackoverflow.com/questions/5187965/…
【解决方案2】:

getElementsByTagName 失败,因为它找不到您要替换的标识符。很可能页面上没有任何 id 为 bottom、user_info 或 notice 的元素。很多时候我不小心设置了 div 的类而不是 id 等。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-12-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多