What is the difference between link_to and link_to_unless_current? - ruby-on-rails

I'm ust a beginner using Ruby on Rails for building website.. Here they have not clearly mentioned the difference between link_to and link_to_unless_current.

link_to will always generate a link.
link_to_unless_current will be ignored if the URL it would link to is the same as the URL that rendered the view containing it.

link_to just generates a link, link_to_unless_current only creates the link if the current page is not equal to the link you provided.
There is also a link_to_unless method, where you can provide a custom condition when to show the link.
For more information take a look at the UrlHelper documentation.

Link_to refers to "redirecting no matter what", and link_to_unless_current redirects unless it is already the current page.

Related

How to fix rails 4 hyperlinks?

I'm trying to display someone's profile links to their social media profiles. My current code is
<p>
<strong>Linkedin:</strong>
<%= link_to #person.linkedin, #person.linkedin %>
</p>
It works and the link does load, but it goes to localhost:3000/user/linkedin.com/in/user instead of linkedin.com/in/username
Thanks!
You need to add make sure the linkedin hyperlinks have a protocol (http, https, etc).
Any link without a protocol is assumed to be a relative path, which is why the hyperlinks are getting appended to your website url.
A solution would be to manually add a "http://" string at the beginning of every person's linkedin hyperlink in your database. Your code should work fine after that.
Edit: Or you can change it on the fly like so (the other answers won't work since it looks like #person.linkedin contains the entire hyperlink not just the linkedin user)
<%= link_to #person.linkedin, "https://#{#person.linkedin}" %>
Rails link helpers follow the format:
link_to(name = nil, options = nil, html_options = nil, &block)
The second #person.linkedin path is a local path as determined by your routes file in your config folder. If the link you need follows a certain format you can do something like
<%= link_to "LinkTextHere", "http://www.linkedin.com/#{#person}/profile" %>
I can answer in more detail if you give me the exact outcome you need as well as what you want from the .linkedin value. Also, typing "rake routes" in your console will show all paths you currently have and can help troubleshoot issues like why #person.linkedin is routing locally.

Rails: How can I write a path to the current page?

How can I create a path that redirects back to the current page?
Eg. I'm on http://example/users/3 , I click on the link, and it redirects me to http://example/users/3.
This seems like it should be very simple, but I can't find anything online and all my guesses return an 'undefined method' error.
I'm trying to print erb into an href link, so I think it has to be a path... something along the lines of
href="facebook/sharer/sharer.php?u=<%= path_to_current_url %>"
If it matters, I'm using Rails 4.0.10.
Try:
request.original_url
This will get the absolute url.
Documentation:
http://api.rubyonrails.org/classes/ActionDispatch/Request.html#method-i-original_url
YOu can use request.env['REQUEST_URI'] to get the url as a string.
You could use <%= link_to "Refresh", '#' %>

how to add link tag in ruby on rails

I need to display another page when I click the link. I am using the following code. But I didn't get the expected output. It says No route matches "/wel.html" with {:method=>:get} what can I do? Please help me.
<%= link_to "Click here", "wel.html" %>
ERB stands for "embedded Ruby", you can't have a link to an ERB file.
If you are pointing to an internal page, you should have a route to that through a controller action to make it actively reachable.
If you are pointing to an external source, you need to provide the full path, i.e. "http://...".
I'd suggest you take a look at a Rails tutorial before starting to build an app.
If you need to open wel.html file in your app, then you need to put it into public folder and change your link to <%= link_to "Click here", "/wel.html" %>.

Including a hash param in Rails URL

I have links on a page which go to pages_path('index').
That works fine, only I want each link to go to a specific section of a page.
So each link would look something like
www.example.com/pages/index#locations
Right now I'm using a rather hacky solution to generate this link
link_to "About", "#{page_path('index')}#about"
Is there a better way to achieve this?
You just need to use the anchor option:
link_to "About", page_path('index', anchor: "about")

How to add link field in migration?

I am trying to add a link field to a form. The link should be clickable when displayed. When I run
rails generate migration AddLinkToThings link:xxxx
What should xxx be? I've tried link:string, but that just puts out a string, of course.
Here is how I am making the title of a Thing link to where the user specifies:
<strong><%= link_to #thing.title, #thing.link %></strong>
The problem is that #thing.link is linking to the show page for the Thing, and not the link that the user typed in. It's ultra confusing to explain.
For example, when I created this Thing, I linked it to google.com, but instead, it's linking to mywebsite.com/things/7. Hope this makes sense.
It would be a string, but then you'd just use it inside of a link_to helper in your view.
Depending on the contents of the string, you may need to append .html_safe to the string inside the link_to tag.
Update
Based on your edit, I think you'll want to interpolate #thing.link inside a string.
Example:
<strong><%= link_to #thing.title, "#{#thing.link.try(:html_safe_}" %></strong>
If link will never be nil, you don't need the try.

Resources