首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >显示Rails 3的旋转器UJS获得类型错误

显示Rails 3的旋转器UJS获得类型错误
EN

Stack Overflow用户
提问于 2011-10-31 04:19:07
回答 2查看 1K关注 0票数 2

我的Rails 3应用程序中有三个链接,使用UJS在它们下面的容器中加载信息。为了完成这个体验,我想展示一下“加载.”当信息加载时,文本。所以我用了一些来自Simone的帖子帖子来尝试实现它。但是,根据jQuery的不同,在单击第一个链接时会发生两种不同的情况。(在这两种情况下,单击其他两种情况都不会发生任何情况。)

  • 给出下面的代码,我得到了TypeError: 'undefined' is not a function (evaluating '$("#tabs-1").js(data)')

为了解决这个问题,我将我的jQuery更改为$("#tabs-1").html(data);。然后:

  • TypeError离开了
  • “装.”不显示
  • 单击我的第一个链接将呈现各种未格式化的、无关的代码,如:
    • $(“#tabs 1”).html(“\n文本:\n\n文本”)

生成的HTML:

代码语言:javascript
复制
<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 操作示例:

代码语言:javascript
复制
def profile_about
  @profile = Profile.find(params[:id])
  respond_to do |format|
    format.js { render :layout => nil }
  end
end

My profile_about.js.erb:

代码语言:javascript
复制
$( "#tabs-1" ).html( "<%= escape_javascript( render (:partial => "profile_about" ) ) %>" );

My application.js:

代码语言:javascript
复制
$(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中的

代码语言:javascript
复制
span#loading {
    float:right;
    padding:10px 5px;
    color:#666666;
}

我的<head>**:**中的

代码语言:javascript
复制
<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&#47;ty9haXTCWxYTOZB4DxQ90uEGTIhNo8="/>

更新:

如果我删除respond_to format.js和format.xml,只保留format.html,删除profile_about.js.erb,并将绑定保持为.html(数据),控制台会这样说:

代码语言:javascript
复制
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
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2011-11-02 01:59:27

我通过将我正在使用的jQuery替换为在这个问题中找到的jQuery来工作。它比我所使用的jQuery要简单得多。

代码语言:javascript
复制
$(function() {
    $('#loading')
        .hide() // hide initially
        .ajaxStart(function(){
            $(this).show();
        })
        .ajaxStop(function(){
            $(this).hide();
        })
});

我还对上述问题中的代码做了一些小改动,以使其正常工作。我特别删除了: css中的display:none,控制器操作中的format.html和format.xml。

票数 2
EN

Stack Overflow用户

发布于 2011-10-31 17:43:52

您目前有三个具有相同ID的链接;这是无效的HTML,将导致问题。

不确定您在.js方面尝试了什么;我甚至不认为这是一个jQuery方法。

您可以使用JS或HTML完成这一任务,但我不建议两者兼而有之--删除xml和js respond_to,只保留html。删除JS erb文件。

在"ajax:success“绑定中,将其保持为.html(data)

假设其他一切都已正确设置,这应该是您需要做的全部工作。

您使用的是哪个版本的Rails?

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/7949978

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档