我正在Rails项目中集成我的第一个Ember应用程序。我添加了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应用程序中,可能缺少了一些非常基本的东西。非常真诚地感谢您能提供的任何帮助。
发布于 2015-10-19 17:07:59
我找到了答案这里
因此,继续将尾随斜杠添加到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这样做,你将受到“欢迎来到安博”作为您的网页的一部分。
发布于 2017-05-26 00:23:21
在Rails的routes.rb中
get 'products', to: redirect('/products/'), constraints: lambda { |req| req.env['REQUEST_URI'].last != '/' }https://stackoverflow.com/questions/33216623
复制相似问题