I am attempting to use asset_path
during development in my Ruby on Rails 4 application via a javascript. In my javascript, I'm referencing an HTML file using something like:
<%= asset_path('templates/login/index.html.erb') %>"
The file physically resides in $RAILS_ROOT/app/assets/templates/login/index.html.erb
When my javascript tries to grab this file, though, it is catching a 'catchall' route I put together because my frontend is AngularJS and it is handling the "routing" for the application. Here is the log:
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)
My routes.rb looks like the following:
App::Application.routes.draw do
devise_for :users
root :to => 'home#index'
namespace :api do
end
get '*a', to: 'home#index'
end
What's the best way to avoid this issue? How can I reference the template file in my javascript/angular project?
As I mentioned in comments below what I'm trying to achieve is to retrieve templates from the asset pipeline instead of having to go to the server to grab them. The additional round-trip doesn't make sense since they can be served at page load to make the app perceived as 'faster' when they're already cached. The issue here is that you still need to define a Rails route to match every route on Angular's side otherwise Rails will return a 404.
asset_path
doesn't load all files in the assets directory. It is meant for images.
If you want to add other files, you need to add a parser for them.
As what you're trying to do is use your angular templates from the assets pipeline though, you should take a look at angular-rails-templates.
It will automaticaly compile your templates into javascript, and make them available to angular.
You can then use them as you usually do. And once you deploy into production, all your templates will be included into your single application.js
file.
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments