Note that there are some explanatory texts on larger screens.

plurals
  1. POWhat's wrong with my Ruby on Rails code?
    primarykey
    data
    text
    <p>So I've been trying to add some AJAX to my home page with rjs and I get this javascript error in a popup window:</p> <p><code>RJS error:</code></p> <p><code>TypeError: Cannot call method 'getElementsByTagName' of null</code></p> <p>Below is all of the relevant code.</p> <p>Here's my code in app/views/microposts/create.rjs:</p> <pre><code>page.insert_html :bottom, :feed_items, :partial =&gt; 'shared/feed_item', :object =&gt; @micropost page.replace_html :user_info, pluralize(current_user.microposts.count, "micropost") page[:micropost_form].reset page.replace_html :notice, flash[:notice] flash.discard </code></pre> <p>Here's app/views/pages/home.rb:</p> <pre><code>&lt;% if signed_in? %&gt; &lt;table class="front" summary="For signed-in users"&gt; &lt;tr&gt; &lt;td class="main"&gt; &lt;h1 class="micropost"&gt;What's happening?&lt;/h1&gt; &lt;%= render 'shared/micropost_form' %&gt; &lt;%= render 'shared/feed' %&gt; &lt;/td&gt; &lt;td class="sidebar round"&gt; &lt;%= render 'shared/user_info' %&gt; &lt;%= render 'shared/stats' %&gt; &lt;/td&gt; &lt;/tr&gt; &lt;/table&gt; &lt;% else %&gt; &lt;h1&gt;Sample App&lt;/h1&gt; &lt;p&gt; This is the home page for the &lt;a href="http://railstutorial.org/"&gt;Ruby on Rails Tutorial&lt;/a&gt; sample application. &lt;/p&gt; &lt;%= link_to "Sign up now!", signup_path, :class =&gt; "signup_button round" %&gt; &lt;% end %&gt; </code></pre> <p>Here's my feed partial app/views/shared/_feed.html.erb:</p> <pre><code>&lt;% unless @feed_items.empty? %&gt; &lt;table id="feed_items" class="microposts" summary="User microposts"&gt; &lt;%= render :partial =&gt; 'shared/feed_item', :collection =&gt; @feed_items %&gt; &lt;/table&gt; &lt;%= will_paginate @feed_items %&gt; &lt;% end %&gt; </code></pre> <p>Here's my feed item partial app/views/shared/_feed_item.html.erb</p> <pre><code> &lt;tr id = "feed_item"&gt; &lt;td class="gravatar"&gt; &lt;%= link_to gravatar_for(feed_item.user), feed_item.user %&gt; &lt;/td&gt; &lt;td class="micropost"&gt; &lt;span class="user"&gt; &lt;%= link_to feed_item.user.name, feed_item.user %&gt; &lt;/span&gt; &lt;span class="content"&gt;&lt;%= feed_item.content %&gt;&lt;/span&gt; &lt;span class="timestamp"&gt; Posted &lt;%= time_ago_in_words(feed_item.created_at) %&gt; ago. &lt;/span&gt; &lt;/td&gt; &lt;% if current_user?(feed_item.user) %&gt; &lt;td&gt; &lt;%= link_to "delete", feed_item, :method =&gt; :delete, :confirm =&gt; "You sure?", :title =&gt; feed_item.content %&gt; &lt;/td&gt; &lt;% end %&gt; &lt;/tr&gt; </code></pre> <p>And lastly here is the relevant part of my microposts controller:</p> <pre><code>class MicropostsController &lt; ApplicationController before_filter :authenticate, :only =&gt; [:create, :destroy] before_filter :authorized_user, :only =&gt; :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 </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload