Rails engine missing host error. Where do set this config? - ruby-on-rails

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).

Related

How to fetch base URL in 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

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.

How to create a url for notification in Rails

i want to send an email to user after he sign-up with code.for ex
http://192.168.1.51:3000/logins/activate/435546dgfd757676657 #link contains in an email
how can i create the above URL in my notifier model.
i know following way
url_for :controller=>'logins', :action=>'activate', :id=>'435546dgfd757676657' , :host=>'http://192.168.1.54:3000'
Which is working properly.
what i want that host should not be hard coded. How can i get host with port in a model.
In controller i can find it using follwing ways
request.host_with_port
Please provide me correct ruby way for doing same.
You can define the host in your environment.rb file.
config.action_mailer.default_url_options = { :host => 'localhost:3000' }
As your host probably changes depending of your environment (development, test, production), it's better to put that config line inside the environment file.
After that, every link in emails will be made with that host. You don't have to provide it in the view anymore.

What's the best way to pass in the domain name of your rails server for mailing?

I have an app that has is up in a few environments i.e. (development, staging, beta, live)
What's the best way to pass in an app's domain name when sending mail, to allow for different domain names depending on the server?
My first thought is to add something in the respective environment.rb files for each one, so config/environments/beta.rb would contain
ActionMailer::Base.smtp_settings[:domain] = 'beta.domain.com'
And config/environments/staging.rb would contain
ActionMailer::Base.smtp_settings[:domain] = 'staging.domain.com'
This feels like I'm doing something so basic that Rails would already have this value lying around, but I haven't found it in any of the places I would normally expect, nor can i find it in the documentation.
What's the best approach to take here?
I usually just pass the value of request.host in to the ActionMailer method.
In your environment files, you want to set:
ActionMailer::Base.default_url_options = { :host => "beta.domain.com" }
If you're using url_for instead of named routes, you also need to specify :only_path => false ... so you don't get relative urls. I generally try to use named routes, however.

Resources