2

我正在將我的第一個Ember應用程序集成到Rails項目中。我已經加入了ember-cli-rails寶石並運行初始化,使我有一個config/initializers/ember.rb文件看起來像這樣:如何在Rails中修復此Ember CLI錯誤?

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

我灰燼的應用是在我的Rails應用程序的根,被稱爲products。除了現在生成默認的Ember應用外,我什麼也沒做。

我在Rails中爲ProductsController創建了一個特定的佈局。它是app/views/products.html.erb。我已經添加了以下行:

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

我還編輯了router.js文件爲灰燼應用,因爲我不是服了在根URL灰燼應用:

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

最後我改變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 
    } 
    }, 

索引頁面出現罰款,此控制器。而且它試圖加載灰燼文件,但是我收到一個錯誤:

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/ 

我意識到我可能失去了一些東西在我這裏第一灰燼應用非常基本的。非常感謝您提供的任何幫助。

回答

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 

做到這一點,你會用「歡迎來到灰燼」迎接您的網頁的一部分。

+0

我也因爲了解到,這是包括Rails應用程序內的灰燼應用程序的老式的方法。 – AKWF

+0

不再適用於Rails 5! – thesubroot

0

在Rails的routes.rb

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