【问题标题】:routing/asset pipeline issue - undefined javascript method路由/资产管道问题 - 未定义的 javascript 方法
【发布时间】:2012-10-19 03:40:13
【问题描述】:

我正在重新使用 Rails(已经有几年了),所以我很确定我只是错过了一些简单的东西。但这也可能是我对 JavaScript 在 Rails 中的工作方式缺乏了解。

我认为我的观点(在 haml 中)设置正确。我已将 javascript 添加到我确保包含在布局中的文件中。 (而且 css 看起来不错。)我只是没有让 javascript 函数(我在网上找到的幻灯片)连接到视图。我正在尝试使用%body{ :onload => setUpslideShow() }

这是我的看法:

%body{ :onload => setUpSlideShow() }

%script{type: 'text/javascript'}
  if ( ! window.CMFmainmenu ) { CMFmainmenu = {}; }
  var s                   = CMFmainmenu;
  s.user_id               = "#{@user.id.to_s}";
  s.partial_mainmenu_path = "/users/" + s.user_id + "/xx/mainmenu";
  s.add_new_senior        = "#{UsersController::ADD_NEW_SENIOR}"
  s.add_new_senior_path   = "#{new_seniors_settings_path(@locale,@user,@senior)}"

#mainmenu

... (edited out to save space) ...

 #slideshow.greenBorder
        #slides
          = image_tag '/assets/newlook/div_1_img.png', class: 'slide'
          %p
            Everyone loves photo albums.
            Get your user ready to brag, and
            populate and arrange albums by
            event, special people, year or any
            category you can imagine.
            =link_to "Learn how.", ''
          = image_tag '/assets/newlook/div_2_img.png', class: 'slide'
          %p
            If typing is difficult for your
            loved one, remove the frustration
            of a typed response. They can
            respond with a voice message, or
            with auto replies that you set up.
            =link_to "Learn how.", ''
          = image_tag '/assets/newlook/div_3_img.png', class: 'slide'
          %p
            Arms too short? You can specify
            the minimum font size used for
            your user's email.
            =link_to "Learn how.", ''
        #slides-controls
          1
          2
          3

这是我的 javascript:

if (! window.CMFuser) { CMFuser = {}; }

$(function() {

    var s = CMFuser;

... (edited out to save space) ...

       // slideshow

       slidePrefix            = "slide-";
       slideControlPrefix     = "slide-control-";
       slideHighlightClass    = "highlight";
       slidesContainerID      = "slides";
       slidesControlsID       = "slides-controls";

       setUpSlideShow =  function()
       {
... lots of code that doesn't get called so I'm leaving it out to save space ...

这就是我认为布局中的相关行:= javascript_include_tag "lib/modernizr-2.0.6.min", "lib/respond.min","application", "users" 其中“用户”是 javascript 文件。当我注释掉 %body{ :onload => setUpslideShow() } 时,javascript 文件出现在 html 源代码中。

错误全文:

NoMethodError in Users#mainmenu

Showing /opt/cmf/app/views/users/mainmenu.html.haml where line #4 raised:

undefined method `setUpSlideShow' for #<#<Class:0x007fe054fa0a28>:0x007fe05594ae20>
Extracted source (around line #4):

2: - # users/mainmenu
3: 
4: %body{ :onload => setUpSlideShow() }

谢谢

【问题讨论】:

    标签: javascript ruby-on-rails ruby-on-rails-3 slideshow asset-pipeline


    【解决方案1】:

    :onload 的值不应该是字符串吗?

    %body{ :onload => 'setUpSlideShow()' }
    

    你确实想生成这个 HTML,对吧?

    <body onload="setUpSideShow()">
    

    【讨论】:

      猜你喜欢
      • 2018-09-01
      • 2015-01-17
      • 1970-01-01
      • 2011-10-07
      • 2014-02-04
      • 2013-05-12
      • 2012-01-23
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多