Simonas
Simonas

Reputation: 93

Ruby on Rails link_to wrong target?

I have a Project model(:link, :title, :content).
My goal is to show users the whole link, and when they click on it, redirect to it.
In my projects#show I have this code:

%h5= link_to @project.link, @project.link

However this redirects to this url:

http://localhost:3000/projects/project_link_here

How could I get link do this:

project_link_here

Upvotes: 0

Views: 101

Answers (2)

Santosh Sharma
Santosh Sharma

Reputation: 2248

Try Following.

%h5= link_to @project.link, project_link_path

in link_to helper, first argument is link name and the second argument is path or url. in your case @project.link is a link name. replace project_link_path to your link path.

Upvotes: 0

Jake
Jake

Reputation: 143

The link_to url option relies on url_for (as explained in the link_to docs). Read the url_for documentation to see what options you can pass in.

To get the full urls, you include http:// in your link. This can be done by storing Project#links with the "http://" protocol or taking it on later like this:

%h5= link_to @project.link, "http://#{@project.link}"

Alternatively, you could pass in the :protocol and :host options and set only_path to false, but you'd need to also store that information in your database.

Upvotes: 1

Related Questions