Reputation: 4049
Mailer file
button_to 'YES', lender_accept_url(borrow), method: :post, id: "accept #{borrow.id}", style: "background-color:green; color: white; width: 40px; display: inline"
button_to 'NO', lender_decline_url(borrow), method: :post, id: "decline #{borrow.id}", style: "background-color:gray; width: 40px; display: inline"
Routes
post 'inventories/:id/accept', to: 'inventories#accept', as: 'lender_accept'
post 'inventories/:id/decline', to: 'inventories#decline', as: 'lender_decline'
However, when I click a button in the email, it takes me to http://example.com/inventories/:id/decline
Why is the URL helper not leading to my own domain? Note, this same partial used in the Mailer file is also used in a regular view page, and on that page, the routes work fine.
Upvotes: 1
Views: 240
Reputation: 84114
Mailers are divorced from the http request/response cycle: they don't know what the current request is (much like your models), and thus what the current host is. They might not be even used from a request (for example if your emails are sent from a cronjob). On the other hand your views can look at the current request to know what the current host so that's why they generate the correct url.
There are 2 ways of solving this:
You can set
config.action_mailer.default_url_options = { host: "example.com" }
in application.rb or the per environment files.
Alternatively set the host explicitly when you call the URL helpers. This would require whoever is sending the email to pass the desired host to the mailer (using request.host
for example). The mailer would store this in an instance variable, for example @host
and then in your view you would use
lender_accept_url(borrow, host: @host)
The downside of this is that sharing views/partials with regular controller actions would require you to set @host there too.
As a halfway house approach you should also be able to set default_url_options
on a per instance attribute
Upvotes: 1