How to fetch base URL in rails? - ruby-on-rails

I need the base url of my rails app in its code. Example if my site is www.abc.com then i want this to be accessible in all controllers. Is there any method to get it?

There are two step to archive :
According to documentation, you can use request.host or request.host_with_port if you also need the port
You can use environment variable to store your BASE_URL, so that when you need to get your base url, you can call it like ENV['BASE_URL'] either in the model, controller or view
You can use figaro gem to help you manage your environment variable

Related

Rails engine missing host error. Where do set this config?

Say I have a Rails Engine called seasons. Say in the main app I'm trying to link to a url inside the seasons engine. I'm trying to do this:
seasons.winter_url
but that throws:
Missing host to link to! Please provide the :host parameter, set default_url_options[:host], or set :only_path to true
Since I guess the engine doesn't have a host configured. I can solve it by doing this:
seasons.winter_url(host: <something>)
but that's ugly. What can I do?
In the main app. Say your engine is called foo.
Create a new initializer. i.e. in config/initializers/foo_engine.rb:
Foo::Engine.configure do |config|
config.routes.default_url_options[:host] = ENV['API_HOST']
end
ENV['API_HOST'] is an environment variable.
Anywhere in your app. You should be able to call the url or path directly using: winter_url or winter_path (no need for seasons.winter_url).

How do I write a full path in a controller in Rails 3?

I need to write the full path so need to know what the rails_root domain is. How do I do that? For example:
string = "{RAILS_ROOT}/vendors/#{#vendor.id}"
What is the equivalent of "RAILS_ROOT" to give me what the full domain is for my application? So that in development it would subsstitute localhost:3000 and on my heroku site the right full domain?
You should always avoid, if possible, hard-coding your path, because it is less flexible and more prone to result in broken links in the future. Plus, you can use Rails routing, which is an elegant way to generate everything cohesively in Rails without any need to create the composite parts yourself.
If you have your routes set up properly, you should be able to call:
link_to "View vendor", vendor_url(#vendor.id)
Vendor_url(#vendor.id) in Rails gives you your full URL, which you can then contain in your string variable. Here's how to generate the routes needed for the above:
# in routes.rb
resources :vendors
Try:
File.realpath(RAILS_ROOT)
You could access the request object. request.host_with_port would give you the hostname and port. request.protocol will give you the protocol (http:// or https://). request.fullpath will give you the path with query params.

ruby on rails *_url returning wrong address

I'm sending emails from rails, and using the *_url helper to generate urls.
However, these links are starting:
https://admin.test.website.co.uk/
(which is what the URL used to start with), whereas they should start:
https://production.test.website.co.uk/
Anyone know what could be going wrong..?
What I think is going on is that you send the e-mails from your admin-interface. When using a generated _url helper, the host is generated from your current host, if present. If you want to override this, you can specify the host yourself using a variable or even hard-code it.
So, for example, instead of using page_url, you could use page_url(:host => 'https://production.test.website.co.uk/')
Good luck!

How to perform URL redirects properly?

In the action class, I have this one at the end of the function:
$this->redirect("/myproject_dev.php/login/");
In which I hard code the development project name in the url. of course it works while I'm doing development but when it comes to production, I need to remove that.
so, how do I find out the project name ("myproject_dev.php") in the code? so that I can do this:
$this->redirect($myProjectDevName . "login/");
or.. is there a smart way to handle this?
thanks.
------ ADDition ---
so, I used the public_path as suggested. but it only returns the root path.
I'm wondering if there is a function that when I run in development mode:
http://mydomain.com/myproject_dev.php/whatever
in its action class:
$this->redirect(public_path('login'));
and result in
http://mydomain.com/myproject_dev.php/login
while if I run in production:
http://mydomain.com/whatever
the action class is unchanged:
$this->redirect(public_path('login'));
it will return this:
http://mydomain.com/login
or.. how to get the application name and symfony environment name?
:)
You should use the symfony routing system and call a routing rule from your routing.yml:
$this->redirect('#login');
This way it becomes separate of your environment and you wan't need to change anything when you're ready to move to production.
you could try
public_path()
http://www.symfony-project.org/api/1_4/UrlHelper#method_public_path
try this put this in your function
$this->redirect("MODULENAME/ACTION");
ex: $this->redirect("pim/admin");
this will redirect you to the pim module admin action...

Rails - How to obtain an absolute URL for the site? https://example.com

For one of my models I have a method:
def download_url
url = xxxxx
end
which works nicely to make /xxxx/xxxx/3
What i want to do is updated this to include an absolute URL so I can use this method in an email:
https://example.com/xxxx/xxxx/3
But I don't want to hard code. I want it to be an environment var so it works on dev & production
Emails are effectively views, and can use helpers. The model shouldn't really have any knowledge about the views - instead, you should use url_for or one of its descendant methods in the email view template to generate a URL. Those helpers can generate absolute URLs based on the location that the application is running (and associated configuration - you'll want to set config.action_mailer.default_url_options[:host] in your environment file) without having to mess with environment variables and the like.
I would define the domain as a constant in development.rb & production.rb:
APP_DOMAIN = "https://mysite.com"
And then just use this constant in your method within the model:
def download_url
"#{APP_DOMAIN}/download/#{id}"
end
It may be ugly, but it's necessary. Rails apps don't and shouldn't know their root URL. That's a job for the web server. But, hardcoding sucks...
If you're using capistrano or some other deployment method, you can define the server host in a variable and write it out to a file that you can read from the app.

Resources