Aaron
Aaron

Reputation: 489

How do you redirect to an external website with rails?

I want links that when hovered over you see the link to look something like this:

http://www.website.com/redirct_to=linkID2 (maybe not exactly like that but try to get the idea)

I have a form area in my blog that I can input a website url but I want to make it redirect_to an external website when it shows the post.

Upvotes: 29

Views: 34293

Answers (4)

Thomas Van Holder
Thomas Van Holder

Reputation: 1517

redirect_to "https://website.com", allow_other_host: true

Upvotes: 11

Ben Hughes
Ben Hughes

Reputation: 14195

redirect_to "https://website.com"

should do it as long as protocol is included. For added flexibility, you can parse it with URI to ensure that all fields are correct. You might want to URI.encode/URI.decode

Upvotes: 60

zenbakiak
zenbakiak

Reputation: 1

redirect_to isn't available in views or is not a helper method, be sure to use it in your controllersbut if you really want to redirect from view, use the javascript solution

window.location.href=<%= post.link_url %>

Upvotes: 0

Simone Carletti
Simone Carletti

Reputation: 176442

Create a new model, for example called Link, to store the URLs you want to redirect to. Then generate a new controller (you can use scaffold to generate controller and model at the same time) and change the show action in order to fetch the record with given ID and redirect_to @link.url.

If you don't want to use the #show action for this step, create a new action do handle the redirect (for example goto, redirect...).

Upvotes: 2

Related Questions