首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何修复Rails中的Ember CLI错误?

如何修复Rails中的Ember CLI错误?
EN

Stack Overflow用户
提问于 2015-10-19 14:08:48
回答 2查看 489关注 0票数 2

我正在Rails项目中集成我的第一个Ember应用程序。我添加了ember-cli-rails gem并运行了初始化程序,这样我就有了一个类似于以下内容的config/initializers/ember.rb文件:

代码语言:javascript
复制
EmberCLI.configure do |c|
  c.app :products, path: 'products'
end

我的Ember应用程序位于我的Rails应用程序根目录中,名为products。目前,我只做了一个默认的Ember应用程序。

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

代码语言:javascript
复制
<%= include_ember_script_tags :products %>
<%= include_ember_stylesheet_tags :products %>

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

代码语言:javascript
复制
var Router = Ember.Router.extend({
  rootURL:  config.baseURL, // added this line
  location: config.locationType
});

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

代码语言:javascript
复制
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文件,但是我收到了一个错误:

代码语言:javascript
复制
Uncaught Error: Assertion Failed: rootURL must end with a trailing forward slash e.g. "/app/"

关于ember-cli-rails的说明不包括尾随斜杠。

如果我加了后面的斜线,我会得到:

代码语言:javascript
复制
Uncaught Error: Assertion Failed: Path /products does not start with the provided rootURL /products/

我意识到,在我的第一个Ember应用程序中,可能缺少了一些非常基本的东西。非常真诚地感谢您能提供的任何帮助。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2015-10-19 17:07:59

我找到了答案这里

因此,继续将尾随斜杠添加到baseURL。然后遵循上面链接的帖子中的说明,我将在此总结如下:

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

这样做,你将受到“欢迎来到安博”作为您的网页的一部分。

票数 1
EN

Stack Overflow用户

发布于 2017-05-26 00:23:21

在Rails的routes.rb

代码语言:javascript
复制
get 'products', to: redirect('/products/'), constraints: lambda { |req| req.env['REQUEST_URI'].last != '/' }
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/33216623

复制
相关文章

相似问题

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