Hilmi Yamcı
Hilmi Yamcı

Reputation: 473

Ruby on Rails renderin a view with static script files

In my ruby on rails application I have controlled called AnimalsController. At this controller I have a method "edit" and a corresponded edit.html.erb view in my animals folder. When I also manually added some static javascript file into the page which are located at public/assets/js/ folder. When I render the view, instead of rails is trying to load these javascripts file in the relative path instead of mydomail.com/assets/js/ folder.

Url: /animals/:id/edit

I have script file myscript.js inside assets/js/

When I send request to htt://0.0.0.0:3000/animals/:id/edit

Rails search for http://0.0.0.0:3000/animals/:id/edit/assets/js/myscript.js

I want it to seach the script at http://0.0.0.0:3000/animals/js/myscript.js

This is code is in my edit.html.erb

<script src="assets/js/myscript.js"></script>

Not: I intentionally don't use <%= javascript_include_tag 'application', 'data-turbolinks-track' => true %>

Any suggestions ?

Thanks.

Upvotes: 1

Views: 635

Answers (2)

lcguida
lcguida

Reputation: 3847

If you use the rails helper, it will handle this for you.

 <%= javascript_include_tag 'myscript' %>

Just don't forget to add this file to the assets initializer config/initializers/assets.rb.

Upvotes: 1

Michael Gorman
Michael Gorman

Reputation: 1077

It should be

<script src="/assets/js/myscript.js"></script>

Without the starting '/' it assumes the asset path is rooted on the page path. With the starting '/' it assumes the asset path is rooted at the domain.

Upvotes: 1

Related Questions