我正在嘗試在我的Ruby on Rails 4應用程序的開發過程中通過javascript使用asset_path
。在我的javascript,我用類似引用的HTML文件:Ruby on Rails 4和asset_path的用法
<%= asset_path('templates/login/index.html.erb') %>"
該文件在物理上位於$RAILS_ROOT/app/assets/templates/login/index.html.erb
當我的JavaScript試圖抓住這個文件,但是,它是捉' catchall'路線我放在一起,因爲我的前端是AngularJS,它正在處理應用程序的「路由」。下面是日誌:
Started GET "/templates/login/index.html.erb" for 127.0.0.1 at 2013-07-30 11:20:43 -0400
Processing by HomeController#index as
Parameters: {"a"=>"templates/login/index.html"}
Rendered home/index.html.erb within layouts/application (0.0ms)
Completed 200 OK in 12ms (Views: 12.3ms | ActiveRecord: 0.0ms)
我的routes.rb如下所示:
App::Application.routes.draw do
devise_for :users
root :to => 'home#index'
namespace :api do
end
get '*a', to: 'home#index'
end
什麼來避免這個問題的最佳方式是什麼?我如何在我的javascript/angular項目中引用模板文件?
正如我在下面的評論中提到的,我試圖實現的是從資產管道中檢索模板,而不必去服務器抓取它們。額外的往返行程沒有意義,因爲它們可以在頁面加載時提供服務,以使應用在已經被緩存時被視爲「更快」。這裏的問題是,你仍然需要定義一個Rails路線來匹配Angular的每一條路線,否則Rails將返回一個404.
你應該檢查出第一個答案在這裏:http://stackoverflow.com/questions/12116476/rails-static-html-template-files-in-the-asset-pipeline-and-caching-in-developmen – coreyward
這並不能真正解決模板網址觸及一個catchall路線的問題。我試圖在這裏避免服務器端處理。 – randombits
我基本上試圖避免AJAX檢索模板。一切都應該從資產管道中獲得。額外的往返服務器似乎是不合理的,但如果路徑不在Rails一側,即使路徑在Angular中指定,也會返回404。 – randombits