links in comments in a rails application - ruby-on-rails

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.

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.

Making A Back Link Going To Same Page Left(Ruby On Rails)

I'm using Kaminari pagination on a site, and when the user leaves the page and clicks the back link it goes back to the first page.
What I want is for the back link to go to the same page left. I know I should use a session variable for this, but I'm pretty new to Ruby On Rails so I'm not 100% positive on the syntax here.
Would love some help.
<%= link_to_function "Back", "history.go(-1);return true;" %>
I should add that it's a good practice to code links to pages as <a> tags rather than creating a form in your HTML for that purpose.
If you need to style the link to look like a button, do that in the CSS.

Rails - Given a block of text, auto-link links

on mysite I have the ability to add comments. Sometimes users enter comments will links (href. ...)
I would like to have those links to be clickable/linkable (a href) when the comment is displayed to users.
how with Rails 3 can I take a comment, and look for links and then wrap those links in an a href tag that opens in a new window?
Thanks
The simplest way is to use the auto_link method built into rails.
Note: In Rails 3.1 auto_link has been moved into a separate gem.
idlefinger's suggestion of #auto_link is perfect. I know it's not the question you originally posed, but wanted to suggest: also check out #simple_format, which will nicely format your users' use of newlines into br and p tags.

Rendering field data as a link in Ruby on Rails

Ok, I think this is probably an easy question but for the life of my I can't figure it out. I have created a table called ugtags and in that table I have two columns (beyond the basics), 'name' and 'link'.
I am trying to allow a user to add a link to a page. Ideally they would enter the link title (name) and the url (link) and in the view it would display the title as a link to the url that was entered in the link column.
I there a way to do it by simply affecting the <%= link_to h(ugtag.name) %> code?
You should just be able to do:
<%= link_to h(ugtag.name), ugtag.link %>
See the documentation for all of the relevant options.

RoR - make links clickable that show up in a user comment

I am looking for a Ruby script that takes a piece of text, and makes any links click-able that show up in it... Does anyone have something like this? For example, if I wrote a comment and said "Come check out my site at http://www.example.com", then the link would be click-able like an html tag.
Thanks,
Josh
Rails has a View helper called auto_link, e.g.:
<%= auto_link("Please visit my site: http://www.example.org/") %>
would produce:
Please visit my site: http://www.example.org/
Update: You can find more information about it here.

Resources