2011-03-03 23 views
-1

所以我一直想一些AJAX添加到我的RJS主頁,我得到一個彈出窗口這個JavaScript錯誤:我的Ruby on Rails代碼有什麼問題?

RJS error:

TypeError: Cannot call method 'getElementsByTagName' of null

下面是所有相關代碼。

這是我在app /視圖/微柱/ 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 

這裏的應用程序/視圖/頁/ 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 %> 

這裏是我的飼料部分的應用程序/意見/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 %> 

這裏是我的源項目的部分應用程序/視圖/共享/ _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 
+1

請在標題中描述您的問題,標題只是泛型的,它可能是我得到一個TypeError:無法調用方法'getElementsByTagName'null軌。 – daniel

回答

1

鑑於這是一個RJS錯誤,那問題就在於create.rjs

嘗試註釋掉page.replace_html :notice, flash[:notice],看看是否能作品。

+0

啊,好吧。現在我沒有js錯誤,但是在刷新頁面之前,新帖子不會出現。我也不再收到閃光信息... –

+0

哦,別介意,這篇文章確實出現了!我如何獲得Flash消息?爲什麼不''page.replace_html:notice,flash [:notice]'工作? –

+1

它不工作的原因是因爲HTML中有(可能)沒有元素,並且有'notice'的id。嘗試在某個地方向頁面添加「

」,取消註釋該行並查看它是否有效。 –

0

getElementsByTagName失敗,因爲它無法找到您要替換的標識符。很有可能您的頁面上沒有任何元素,其中包含bottom,user_info或notice。很多時候我不小心設置了div的類但不是id等