2

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.

randombits
  • 47,058
  • 76
  • 251
  • 433
  • You should probably check out the first answer here: http://stackoverflow.com/questions/12116476/rails-static-html-template-files-in-the-asset-pipeline-and-caching-in-developmen – coreyward Jul 30 '13 at 15:47
  • That doesn't really solve the problem of template URLs hitting a catchall route. I'm trying to avoid server-side processing here. – randombits Jul 30 '13 at 15:59
  • I'm basically trying to avoid AJAX to retrieve templates. Everything should be available from the asset pipeline. The additional round-trip to the server seems non-sensical, but if the route isn't there on the Rails side, a 404 will be returned even though the route is specified in Angular. – randombits Jul 30 '13 at 16:06
  • It sidesteps the issue of using a catchall route at all, opting instead to create a controller for handling the delivery of the templates. I'm not familiar with Angular so I'm not sure the specific needs as far as templates go. – coreyward Jul 30 '13 at 16:17

1 Answers1

4

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.

Damien MATHIEU
  • 31,924
  • 13
  • 86
  • 94
  • 1
    This looks OK, but I'd really prefer to use something that has a larger community behind it. Not sure if this is maintained or not and documentation is rather thin. There has to be a clear cut solution. – randombits Jul 30 '13 at 16:01
  • @randombits You should look a little closer at the author of the library; you may have bumped into him recently on StackOverflow. And as far as whether it's maintained — you can easily check out the commit history and see it's only a 2-week old project. ;) – coreyward Jul 30 '13 at 16:19
  • I'm aware that the author of this answer created the library. That still doesn't change my original thought. – randombits Jul 30 '13 at 18:33
  • Use it or don't, that's up to you. The main point of my answer is that you cannot just use asset_path like you do. And though adding a parser is not complicated, it's not just 5 lines. – Damien MATHIEU Jul 30 '13 at 18:56
  • 1
    Damien - do you currently have an example with this gem in the works with a simple "hello world" template solution? – randombits Jul 30 '13 at 20:52
  • This actually looks good and I see window.AngularRailsTemplates defined, just not sure how to access the templates. I do see this documentation "In your application, add a dependency to the templates module." would be great if that was further elaborated on? Thanks again for the help. – randombits Jul 30 '13 at 21:13
  • Thank you for your comment. I've added a bit more of explanation in the README. I hope it's clearer now. – Damien MATHIEU Jul 31 '13 at 09:43