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.
Related
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).
I want to place an image in emails to let me know if a person has opened the email. So the image will simply be a route in my rails app to a specific controller to handle that logic:
<img src="http://www.example.com/invitations/43/open.png" />
Problem is, http://www.example.com works in production, but we have different development and test environments. Is there a way to use an environment configuration value in the /config/environments Ruby files inside an ActionMailer template?
You could make use of the action_mailer.default_url_options if you set it in your config files (config/environnments/).
If you set in production.rb and developpment.rb like so
config.action_mailer.default_url_options = { host: 'www.example.com' }
You can call it anywhere using
Rails.application.config.action_mailer.default_url_options[:host]
When sending emails (for example for 'Reset password instructions') using Devise, i need to know current domain URL, and set that value into mailer's template.
request.url doesn't work for me.
Assuming, this Rails application is accessibly from multiple URLs.
Any ideas?
Request object is not available in the mailers. You will need to set up the host in the environment configuration file with something like:
ActionMailer::Base.default_url_options[:host] = 'myhost.com'
My application sends E-mails containing absolute urls.
I set host in config/environment/development.rb
config.action_mailer.default_url_options = { :host => 'localhost:3000' }
Now I want to test if Email contains valid url. Using regular expression I take out full url from E-mail and want to visit this address using Capybara function.
mail = ActionMailer::Base.deliveries.last
address = mail.body.to_s[%r{http.+/edit}]
visit address;
But I don't know what host should be set in config/environment/test.rb
When I set localhost:3000 it tries to connect to my local server started by rails server command.
Do you have any ideas to solve this problem?
What worked for me:
Capybara.server_port = 3005
(see https://github.com/vangberg/capybara/commit/5784f03d6aa87e63e759abda794a43738e4f320f)
and
config.action_mailer.default_url_options = { :host => 'localhost:3005' }
To enable this to work when hardcoding Capybara ports is not an option (eg. parallel specs), put this in your spec_helper.rb
YourApp::Application.config.action_mailer.default_url_options[:host] = "localhost:#{Capybara.server_port}"
Instead of visiting the URL why not just validate it against a RegEx, make sure that it is valid, encoded correctly, points to the right controller.
You probably don't want to actually interact with the server, and if you do not the production server so localhost:3000 is a goo option
Hey here is scary code snippet that maybe could help you. I use email_spec to simplify working with email in specs and cucumber scenarios and it already has some helpers to simplify your task. In my application I have a little more complexer situation so I was forced to write my own parser. Here is the code. Enjoy:)
in my config/application.rb file I have a line:
config.action_mailer.default_url_options = { :host => 'example.com' }
How can I configure the host attribute for default url options automaticaly from the domain, where my Rails app is hosted ?
It has to be explicitly specified somewhere because how else would rails know what the domain is?.. A request through the browser (or curl, or whatever) will have a host which rails knows about, but if you load up rails console, what would the domain be?
If you're using something like capistrano, you could write a task to create an initializer which sets the mailer url depending on the host(s) you've set in your deploy recipe.