我的Rails 3应用程序中有三个链接,使用UJS在它们下面的容器中加载信息。为了完成这个体验,我想展示一下“加载.”当信息加载时,文本。所以我用了一些来自Simone的帖子的帖子来尝试实现它。但是,根据jQuery的不同,在单击第一个链接时会发生两种不同的情况。(在这两种情况下,单击其他两种情况都不会发生任何情况。)
TypeError: 'undefined' is not a function (evaluating '$("#tabs-1").js(data)')为了解决这个问题,我将我的jQuery更改为$("#tabs-1").html(data);。然后:
生成的HTML:
<div id="tabs">
<ul id="infoContainer">
<li><a href="/profiles/1/profile_reviews" class="active" data-remote="true" id="profile_loader">Cred</a></li>
<li><a href="/profiles/1/profile_about" class="inactive" data-remote="true" id="profile_loader">About</a></li>
<li><a href="/profiles/1/profile_credits" class="inactive" data-remote="true" id="profile_loader">Credits</a></li>
<span id="loading">Loading...</span>
</ul>
<div id="tabs-1">
# load info here
</div>
</div>加载信息的profiles_controller.rb 操作示例:
def profile_about
@profile = Profile.find(params[:id])
respond_to do |format|
format.js { render :layout => nil }
end
endMy profile_about.js.erb:
$( "#tabs-1" ).html( "<%= escape_javascript( render (:partial => "profile_about" ) ) %>" );My application.js:
$(function() {
var toggleLoading = function() { $("span#loading").toggle() };
$("#profile_loader")
.bind("ajax:loading", toggleLoading)
.bind("ajax:complete", toggleLoading)
.bind("ajax:success", function(event, data, status, xhr) {
$("#tabs-1").js(data);
});
});我的CSS中的:
span#loading {
float:right;
padding:10px 5px;
color:#666666;
}我的<head>**:**中的
<script src="/javascripts/jquery.js?1316133561" type="text/javascript"></script>
<script src="/javascripts/jquery-ui.js?1316133957" type="text/javascript"></script>
<script src="/javascripts/jquery-ujs.js?1316133561" type="text/javascript"></script>
<script src="/javascripts/application.js?1317960952" type="text/javascript"></script>
<meta name="csrf-param" content="authenticity-token"/>
<meta name="csrf-token" content="k1RB3GSMFweZ/ty9haXTCWxYTOZB4DxQ90uEGTIhNo8="/>更新:
如果我删除respond_to format.js和format.xml,只保留format.html,删除profile_about.js.erb,并将绑定保持为.html(数据),控制台会这样说:
ActionView:MissingTemplate (Missing template profiles/profile_about with {:handlers=>[:rxml, :builder, :rjs, :erb, :rhtml], :locale=>[:en, :en], :formats=>[:html] in view paths "/Users/me/Desktop/app/views", "/Users/me/Desktop/app/vendor/plugins/paperclip/app/views"):
app/controllers/profiles_controller.rb:87:in 'profile_about' #format.html
app/controllers/profiles_controller.rb:86:in 'profile_about' #the respond_to to |format|
Rendered /Library/Ruby/Gems/1.8/gems/actionpack-3.0.5/lib/action_dispatch/middleware/templates/rescues/missing_template.erb within rescues/layout发布于 2011-11-02 01:59:27
我通过将我正在使用的jQuery替换为在这个问题中找到的jQuery来工作。它比我所使用的jQuery要简单得多。
$(function() {
$('#loading')
.hide() // hide initially
.ajaxStart(function(){
$(this).show();
})
.ajaxStop(function(){
$(this).hide();
})
});我还对上述问题中的代码做了一些小改动,以使其正常工作。我特别删除了: css中的display:none,控制器操作中的format.html和format.xml。
发布于 2011-10-31 17:43:52
您目前有三个具有相同ID的链接;这是无效的HTML,将导致问题。
不确定您在.js方面尝试了什么;我甚至不认为这是一个jQuery方法。
您可以使用JS或HTML完成这一任务,但我不建议两者兼而有之--删除xml和js respond_to,只保留html。删除JS erb文件。
在"ajax:success“绑定中,将其保持为.html(data)。
假设其他一切都已正确设置,这应该是您需要做的全部工作。
您使用的是哪个版本的Rails?
https://stackoverflow.com/questions/7949978
复制相似问题