How do I defined a variable link_to to an external URL How do I defined a variable link_to to an external URL ruby-on-rails ruby-on-rails

How do I defined a variable link_to to an external URL


It sounds like you are storing URLs without the http:// so they are being interpreted as relative URLs. You just need to do something like this:

link_to micropost.website, "http://#{micropost.website}"

or maybe add a full_url method to that model that adds it if it's missing.

By the way, you can't use @micropost in that partial because it doesn't exist (you only have @microposts or micropost).


You can try with this below code:

<%= link_to "your label", "your link with http", :target => "_blank" %>

This will create a link that opens in a new tab.


You can do something like that:

link_to micropost.website, url_for(micropost.website)

See Rails Api: url_for

You can experiment in rails console.Just type in console:

micropost = Micropost.firsthelper.link_to micropost.website, url_for(micropost.website)

And you see a result string.

Also you need to learn the difference between path and url helpers.See ruby on rails guide.

Goro rights. You need to add "http://" to your website attribute.After validating and before save Model instance to database you need to add this prefix.