【问题标题】:How can I fix this Ember CLI error in Rails?如何在 Rails 中修复此 Ember CLI 错误?
【发布时间】:2017-05-26 08:23:21
【问题描述】:

我正在将我的第一个 Ember 应用程序集成到 Rails 项目中。我添加了ember-cli-rails gem 并运行了初始化程序,这样我就有了一个看起来像这样的config/initializers/ember.rb 文件:

EmberCLI.configure do |c|
  c.app :products, path: 'products'
end

我的 Ember 应用程序位于我的 Rails 应用程序根目录中,名为 products。除了生成默认的 Ember 应用程序之外,我什么也没做。

我在 Rails 中为ProductsController 创建了一个特定的布局。这是app/views/products.html.erb。我添加了以下几行:

<%= include_ember_script_tags :products %>
<%= include_ember_stylesheet_tags :products %>

我还编辑了 Ember 应用程序的 router.js 文件,因为我没有在根 URL 处提供 Ember 应用程序:

var Router = Ember.Router.extend({
  rootURL:  config.baseURL, // added this line
  location: config.locationType
});

最后我在 Ember 应用中更改了 config/environments.js

var ENV = {
  modulePrefix: 'products',
  environment: environment,
  baseURL: '/products', // changed from '/' to '/products'
  locationType: 'auto',
  EmberENV: {
    FEATURES: {
      // Here you can enable experimental features on an ember canary build
      // e.g. 'with-controller': true
    }
  },

这个控制器的索引页面很好。它正在尝试加载 Ember 文件,但我收到错误消息:

Uncaught Error: Assertion Failed: rootURL must end with a trailing forward slash e.g. "/app/"

ember-cli-rails 的说明不包含尾部斜杠。

如果我添加了斜杠,我会得到:

Uncaught Error: Assertion Failed: Path /products does not start with the provided rootURL /products/

我意识到我在这里的第一个 Ember 应用程序中可能遗漏了一些非常基本的东西。非常感谢您提供的任何帮助。

【问题讨论】:

    标签: ruby-on-rails ruby-on-rails-4 ember.js ember-cli ruby-on-rails-4.2


    【解决方案1】:

    我找到了答案here

    所以继续在baseURL 中添加斜杠。然后按照上面链接的帖子中的说明进行操作,我将在这里总结:

    class YourEmberAppController
    
      before_action :ensure_trailing_slash
    
      respond_to :html, :js
    
      def index
        render :index
      end
    
      def vehicles
      end
    
      def save
      end
    
      private
    
      def ensure_trailing_slash
        unless trailing_slash?
          redirect_to url_for(params.merge(trailing_slash: true)), status: 301
        end
      end
    
      def trailing_slash?
        request.env['REQUEST_URI'].match(/[^\?]+/).to_s.last == '/'
      end
    
    end
    

    执行此操作,您将在页面中看到“欢迎使用 Ember”。

    【讨论】:

    • 从那以后我也了解到这是一种在 Rails 应用程序中包含 Ember 应用程序的老式方法。
    【解决方案2】:

    在 Rails 中的routes.rb:

    get 'products', to: redirect('/products/'), constraints: lambda { |req| req.env['REQUEST_URI'].last != '/' }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-08-18
      • 1970-01-01
      • 2021-10-01
      • 2015-02-13
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多