4

How to generate urls for mails with an application in API mode?

I want to generate a URL of type: "host/confirmation/token"?

I tested:

<%= url_for("confirm/#{@token}") %>

Result:

confirm/JtuQW54DkZXtAnhm

But host are not present, how to put "host" inside ?

I configured:

  config.action_mailer.default_url_options = {
     host: "lol.com"
  }
1
  • url_for definition says that only_path is true by default, did you tried only_path: false as params of url_for call? Commented Dec 30, 2017 at 23:31

2 Answers 2

3

You just have to set :only_path to false on your url_for call, like:

<%= url_for({ action: "confirm", token_id: @token, controller: "controller_name", only_path: false }) %>
Sign up to request clarification or add additional context in comments.

4 Comments

config.action_mailer.default_url_options = { host: "lol.com", only_path: false } I tested this but same problem
not on your default_url_options, but on your url_for call
ActionView::Template::Error: wrong number of arguments (given 2, expected 0..1), Impossible
just edited my answer with the right syntax, but basically, you have to define action, controller and params (if the url contain a variable, like create routes that accepts ids, you can pass this as id:. You can also always read more on rails api dock
1

You did not put your routes file, I don't know how to work routes file.

You can follow something like this

url_for controller: 'tasks', action: 'testing', host: 'lol.com'
# => 'http://lol.com/tasks/testing'

For more explanation go to API

4 Comments

ActionView::Template::Error: No route matches, its API mode, the url is different between the server and the client, i would like generate URL depending on "production" or "developpemnt" env
That's why need to check your routes file
routes files containes routes for API, no routes for client, the link in email redirect to CLIENT, no API. url_for mapping for routes, the routes on client are different, do u understand ?
I&#39;m sorry I didn't understood this before, but in this case I will use something different than url_for to do that. Try to save your URL domain in some variable or method defined on application controller and get it when build the route

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.