最常见的原因是ember模板无法在rails app中呈现

时间:2013-06-12 22:55:06

标签: javascript ruby-on-rails ember.js

我知道这是一个广泛的问题,但我没有找到具体的答案,现在我觉得我只需要拉霰弹枪的方法。

我正在尝试在rails应用程序中加载一个ember应用程序。我有ember_controller.rb:

class EmberController < FrontendController
  layout 'ember'
  def load
  end
end

我的Rails路线如下所示:

MdmFrontEnd::Application.routes.draw do

  resources :sessions, only: [:create, :new, :destroy]

  match '/signin', to: 'sessions#new'
  match '/signout', to: 'sessions#destroy', via: :delete
  match '*path' => 'ember#load'
  root to: 'ember#load' #makes the root of the app, the ember root.
end

我的rails布局如下所示:

!!! 5
%html
  %head
    %meta{charset:"utf-8"}
    %title= t("layouts.mobile_manager")
    %meta{name:"viewport", content:"width=device-width, initial-scale=1, maximum-scale=1, user-scalable=0" }
    %link{rel: "icon", type:"image/png", href:"/assets/favicon_16x16.png"}
    = javascript_include_tag "i18n"
    :javascript
      window.locale = "<%= I18n.locale %>";
    = stylesheet_link_tag 'application', media: 'all' 
    = csrf_meta_tags 
    <!--[if lt IE 9]>
    <script src="http://html5shim.googlecode.com/svn/trunk/html5.js"></script>
    <![endif]-->

    -if Rails.env.production? 
      :javascript
        var _gaq = _gaq || [];
        _gaq.push(['_setAccount', 'UA-33246783-1']);
        _gaq.push(['_trackPageview']);
        (function() {
          var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
          ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
          var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
        })();
    = render 'shared/js_constants'
    = javascript_include_tag 'app_ember'
  %body

我正在app_ember.coffee初始化一个新的ember应用程序,其中包含以下内容:

#= require jquery
#= require jquery_ujs
#= require handlebars
#= require ember
#= require_self
#= require_tree ./app/routes
#= require_tree ./app/controllers
#= require_tree ./app/models
#= require_tree ./app/templates
#= require_tree ./app/views

window.Mdm = Ember.Application.createWithMixins
  LOG_TRANSITIONS: true

  ajax: ->
  $.ajax.apply(this, arguments)

  getUrl: (path) ->
    "/#{path}"

然后我有我的模板,application.hbs和index.hbs,在应用程序中有{{outlet}}。

但是当我尝试渲染根时,我只是得到一个空白屏幕。

我知道它正在看模板,因为在我运行Ember.TEMPLATES的控制台中它列出了每个模板对象。

我觉得这是一个问题b / t rails和ember但我不知道。

感谢您的任何想法!

编辑::我的控制台

enter image description here

正如您所看到的,它正在加载我的模板对象,但它也说它无法找到应用程序或索引模板。我的模板目录名称和位置与其他目录一样 - 模型,控制器等。

1 个答案:

答案 0 :(得分:1)

这里有很多事情,很难说肯定。有些事情需要考虑

  1. 如果您的ember应用已正确启动,您应该会看到一些console.log输出。这将包括来自ember re的DEBUG信息:已加载的版本。如果您没有看到,那么您的环境就会出现问题。

  2. 由于您已设置LOG_TRANSITION: true,因此当路由器转换为当前路由时,您应该看到console.log消息,如:Transitioned into 'post.index'。如果您在页面加载时没有在控制台中看到任何这些消息,则该应用程序无法正确启动。

  3. 通常当我遇到这种问题时,这是因为我的命名约定与ember所期望的不一致。因此,我的代码被忽略,而ember正在使用生成的默认值。要了解幕后发生的情况,请在创建应用程序时再设置两个属性:

  4. -

    LOG_ACTIVE_GENERATION: true,
    LOG_VIEW_LOOKUPS: true
    

    有了这个,只要ember生成一个Route / Controller或呈现一个视图,你应该看到console.log输出。完成所有这些后,您的console.log应如下所示:

    DEBUG: -------------------------------
    DEBUG: Ember.VERSION : 1.0.0-rc.4 
    DEBUG: Handlebars.VERSION : 1.0.0-rc.4
    DEBUG: jQuery.VERSION : 1.8.3
    DEBUG: ------------------------------- 
    generated -> route:post.index Object {fullName: "route:post.index"}
    Rendering application with <App.ApplicationView:ember785> Object {fullName: "view:application"} 
    Rendering post with <App.PostView:ember789> Object {fullName: "view:post"} 
    generated -> controller:post.index Object {fullName: "controller:post.index"} 
    Rendering post.index with default view <Ember._MetamorphView:ember796> Object {fullName: "view:post.index"}
    Transitioned into 'post.index' 
    

    在这里,您可以看到正在生成哪些类,正在呈现哪些模板以及正在使用哪些视图类。