Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you redirect to an external website with rails?

Tags:

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.

like image 503
Aaron Avatar asked Jun 22 '09 19:06

Aaron


People also ask

How to redirect to external URL in rails?

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.

What is the difference between render and redirect in rails?

Render tells Rails which view or asset to show a user, without losing access to any variables defined in the controller action. Redirect is different. The redirect_to method tells your browser to send a request to another URL.

How do I redirect back in rails?

In Rails 4. x, for going back to previous page we use redirect_to :back. However sometimes we get ActionController::RedirectBackError exception when HTTP_REFERER is not present. This works well when HTTP_REFERER is present and it redirects to previous page.


1 Answers

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

like image 193
Ben Hughes Avatar answered Oct 11 '22 11:10

Ben Hughes