Rails: How can I write a path to the current page? - ruby-on-rails

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", '#' %>

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.

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" %>.

Create Link to External Website

I am trying to create an external link to each individual listing's assigned website address. Using the following code: (The listing website is saved as google.com)
External Link
Takes me to:
localhost:3000/google.com
Is there any way to generate a link that would go to www.google.com instead of trying to find a route in my application.
The reason why it's bringing you to localhost:3000/google.com it's probably because the string you are passing to the href attribute is not a full qualified URL.
In fact, if in HTML you write
External Link
The string will be appended to the current page path. You should make sure that the input you pass always contains the schema. If the input never contains that, then you can assume it's http://
External Link
But this is not really a solution, just a workaround. You should definitely make sure that when you populate the website URL, you store a complete URL. In fact, some sites may require https.
In Rails you normally use the url_for and link_to helpers to generate an URL, but they will both cause the same issue unless you pass a full URL.
<%= link_to "External Link", "http://#{listing.website}" %>
Do it the Rails way:
<%= link_to 'External Link', "http://#{listing.website}" %>
You need to put in the protocol.
Google
Do you get it? =)
You can create link like this:
External Link

What is the difference between link_to and link_to_unless_current?

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.

links in comments in a rails application

I have a rails application where I allow users to enter comments. The comments are displayed back like so
<%= simple_format(sanitize(c.comment)) %>
If a user enters the following the in the comment link this link gets appended to the end of the rails root directory. So if someone clicked on the link the would go to
www.somedomain.com/myrailsapp/www.blah.com
What can I do to correct this?
Thanks
You will need to append "http://" in the href attribute of the anchor tags.
And if you aren't using it, may I suggest the auto_link helper method. It will automatically do what you are looking for.

Resources