Generate Full Url to JavaScript in Rails (Similar to JavaScript_Path, But Url)

Generate full url to javascript in rails (similar to javascript_path, but url)

Absolute URLs to javascript and css files are now available in Rails 4 in ActionView::Helpers::AssetUrlHelper module via Asset Pipeline. In your case specifically it's javascript_url and its alias url_to_javascript and corresponding methods for stylesheets.

And the result is exactly as you mentioned in your question:

javascript_url 'main' # -> 'http://localhost:3000/javascripts/main.js'

Though the question was asked ages ago, hope it will help people seeking answer to the same question in future.

How do I construct an absolute URL in a js.erb file?

Ok, now that I understand your question, I'll answer according....

First option:

Edit your files:

  • config/environments/development.rb
  • config/environments/test.rb
  • config/environments/production.rb

and you have to write the host like:

default_url_options[:host] = 'www.example.com'

then you can create a javascript within your assets like app/assets/javascript/test.js.erb, make sure to add the .erb extension so ruby code is executed first when compiling the javascript and add the code:

<% environment.context_class.instance_eval { include Rails.application.routes.url_helpers } %>
var absURL = '<%= root_url %>';

Have in mind that because its an asset, it will be precompile when deploying once, and the minified version will be served for every request (so the ruby code will be executed just when deploying).


Option 2:

you can add a route of your choice in config/routes.rb like:

match 'my_js', to: 'home#my_js'

and then you can create a view in your home controller: app/views/home/my_js.js.rb and add the ruby code right there:

var absURL = '<%= root_url %>';

Be sure to include the actionpack-page_caching gem in order to generate a cache for your action, so it gets compiled on first request and then it will just serve the compiled version on all the other requests (although it won't minify the javascript, so the assets may be a better solution)

Create a full url link in Action Mailer without a url helper

If you set the action_mailer options

link_to 'Some link',  Rails.configuration.action_mailer.default_url_options[:host] + '/#/some/custom/path'

Will give you 'example.com/#/some/custom/path'

js-routes adding (::format) to urls

Sorry I thought I posted my solution in here.

The problem wasn't js-routes but rather my AJAX call, I was setting the content type to JSON and I assumed that it was converting my object to JSON using the built in methods. This is not true, you need to manually convert the object to JSON via JSON.stringify(obj).

Old ajax call:

....
url: Routes.feedback_path(),
dataType: 'json',
contentType: 'application/json',
type: 'POST',
data: obj,
....

New ajax call:

....
url: Routes.feedback_path(),
dataType: 'json',
contentType: 'application/json',
type: 'POST',
data: JSON.stringify(obj),
....

How to use rails paths in js files for GET's and ajax calls?

This worked for me:

$.get("<%= Rails.application.routes.url_helpers.toggle_full_details_path %>"); 

I don't want any user message if the call fails, hence no $.ajax and success: or failure: calls.

If I had wanted additional stuff for success or failure one could use something like:

$.ajax({
url: "<%= Rails.application.routes.url_helpers.toggle_full_details_path %>",
type: 'GET',
success: function(r) {
$('span#messages').html('<span class="ok">yes</span>');
},
error: function(r) {
$('span#messages').html('<span class="not_ok">no</span>');
}
});

Generate URL for file in /public in Rails 2 ERB view

'<%= ENV["RAILS_RELATIVE_URL_ROOT"] %>/myfile.kml'


Related Topics



Leave a reply



Submit