我的 Ruby on Rails 代码有什么问题?
因此,我一直在尝试使用 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
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:
RJS error:
TypeError: Cannot call method 'getElementsByTagName' of null
Below is all of the relevant code.
Here's my code in 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
Here's 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 %>
Here's my feed partial 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 %>
Here's my feed item partial 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>
And lastly here is the relevant part of my microposts controller:
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
鉴于这是一个 RJS 错误,问题可能出在
create.rjs
中。尝试注释掉
page.replace_html :notice, flash[:notice]
看看是否有效。Given that it's an RJS error, the problem would lie in
create.rjs
Try commenting out
page.replace_html :notice, flash[:notice]
to see if that works.getElementsByTagName 失败,因为它找不到您尝试替换的标识符。很可能您的页面上没有任何 id 为bottom、user_info 或notice 的元素。很多时候我不小心设置了 div 的 class 而不是 id 等。
getElementsByTagName fails because it cannot find the identifier you are trying to replace. Most likely you do not have any elements on the page with the id of bottom, user_info or notice. Often times I have accidentally set the div's class but not the id, etc.