Javascript 什么';我的RubyonRails代码有什么问题?
因此,我一直在尝试使用rjs将一些AJAX添加到我的主页中,在弹出窗口中出现了以下javascript错误:Javascript 什么';我的RubyonRails代码有什么问题?,javascript,ruby-on-rails,ruby,ajax,rjs,Javascript,Ruby On Rails,Ruby,Ajax,Rjs,因此,我一直在尝试使用rjs将一些AJAX添加到我的主页中,在弹出窗口中出现了以下javascript错误: RJS错误: TypeError:无法调用null的方法“getElementsByTagName” 以下是所有相关代码 以下是我在app/views/microscops/create.rjs中的代码: page.insert_html :bottom, :feed_items, :partial => 'shared/feed_item', :object => @mic
RJS错误:
TypeError:无法调用null的方法“getElementsByTagName”
以下是所有相关代码
以下是我在app/views/microscops/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 %>
发生了什么事?
示例应用程序
这是网站的主页
示例应用程序。
“注册按钮回合”%>
以下是我的订阅源部分应用程序/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 %>
“共享/feed\u items',:collection=>@feed\u items%>
这是我的订阅源项目部分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>
五年前发的。
:删除,
:confirm=>“确定吗?”,
:title=>feed\u item.content%>
最后是我的microposts控制器的相关部分:
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
class MicropostsController[:create,:destroy]
在\u筛选器:授权\u用户之前,:only=>:destroy
def创建
@micropost=当前用户.micropost.build(参数[:micropost])
回应待办事项|格式|
如果@micropost.save
flash[:success]=“创建了Micropost!”
format.html{重定向到根路径}
format.js
其他的
@feed_items=[]
呈现“页面/主页”
结束
结束
结束
鉴于这是一个RJS错误,问题在于create.RJS
请尝试注释掉page.replace\u html:notice,flash[:notice]
以查看是否有效。getElementsByTagName失败,因为它找不到您尝试替换的标识符。很可能您在页面上没有id为bottom、user\u info或notice的任何元素。我经常不小心设置了div的类,但没有设置id,等等。啊,好的,是的。现在我没有js错误,但是直到我刷新页面,新帖子才会出现。我也不再收到flash消息…哦,没关系,帖子确实出现了!但是,我如何获得flash消息?为什么page.replace\u html:notice,flash[:notice]
不起作用?它不起作用的原因是(可能)html中没有id为notice
的元素。尝试将
添加到您的页面的某个位置,取消对该行的注释,看看它是否有效。我尝试将其添加到我的应用程序布局中呈现flash的位置,但无法通过id获取。我添加了一个关于此的问题:请在标题中描述您的问题,标题仅为泛型,可能是因为我得到了一个TypeError:无法在Rails中调用null的方法'getElementsByTagName'。