Ruby on Rails 4 and usage of asset_path

randombits

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.

Damien MATHIEU

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.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Rails 4: undefined method `asset_path'

From Dev

Rails 4 Asset Pipeline: Asset missing fingerprint in asset_path from js

From Dev

Rails 4 asset pipeline doesn't overwrite assets with asset_path references to other assets

From Dev

Handlebars + Images in Rails 4 - How do I get the right asset_path?

From Dev

asset_path in rails console on heroku for images

From Dev

Naming/Capitalization usage in Ruby on Rails 4

From Dev

Asset Path Error in Spree / Ruby on Rails

From Dev

Ruby on Rails 4 Routing/Views/Path

From Dev

How can I access Rails asset_path in a JST Underscore template?

From Dev

Add a new folder to asset path in rails 4

From Dev

Rails 4 asset or image path from CSS

From Dev

In Ruby on Rails, what is an "asset"?

From Dev

Ruby on Rails 4 and Paperclip

From Dev

Associations in Ruby on Rails 4

From Dev

Ticketee Ruby on Rails 4

From Dev

ruby on rails and bootstrap 4

From Dev

Ruby on Rails: validation_presence_of usage

From Dev

Rails4 Sprockets; Asset logical path has no extension

From Dev

Rails 4, Asset Compiling

From Dev

Ruby on Rails 4 javascript not executed

From Dev

Ruby on Rails 4 select multiple

From Dev

DateTime formatting in Ruby on Rails 4

From Dev

Designing Ruby On Rails 4 Routes

From Dev

Ruby on Rails 4, stylesheets and pictures

From Dev

Ruby on Rails 4 - Simplify concat

From Dev

Designing Ruby On Rails 4 Routes

From Dev

Search on Ruby On Rails 4 [Help]

From Dev

Ruby on Rails 4, stylesheets and pictures

From Dev

Trying to install ruby and ruby on rails 4 on Ubuntu?