How to fix rails 4 hyperlinks? - ruby-on-rails

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.

Related

How to combine Angular and Rails 4 routes

I have a navbar. On this navbar are four links. Three of them are angular links that look like this:
<span><%= t "navbar.mystream" %></span>
<span><%= t "navbar.mystream" %></span>
<span><%= t "navbar.mystream" %></span>
One is a rails link that deals with user accounts and so I want to keep it outside the scope of my angular app for the time being. It looks like this:
<%= link_to edit_user_registration_path do %>
<span><%= t "navbar.account" %></span>
<% end %>
The links work fine until I go to the rails path which takes me to:
http://localhost:3000/users/edit
Now if I click one of the angular links it just adds the '#' to the url rather than removing the 'users/edit part and adding the # as it does otherwise.
How do I get around this?
The problem, I'm sure you can see, is that without Angular loaded on your account page, its routing can't catch the hashed #path changes. If your URL structure remains simple, you can probably get past that by rendering the full URL (including hash pieces) in the header. That is,
http://localhost:3000/#
http://localhost:3000/#teach
...
http://localhost:3000/users/edit
On your Angular pages, the only part that changes is the hash, and browsers usually won't refresh the page when that happens. Moving between Angular pages and your user account is a real path change that the browser will notice and refresh.
If that doesn't work out, or if your URLs get more complicated, you might need to render different headers depending on the page. Eg, only hashes in the Angular header and full paths in the non-Angular header. That's bound to get fiddly and irritating as your backend learns more about the frontend and becomes entangled with it.
The opposite approach is to just run Angular on every page. You can still do your account editing without it (typical form submissions, etc.), but loading your Angular app alongside should allow the router to catch links the way you want.

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

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.

is it possible to show only a part of the image URL in rails?

I am using Carrierwave and S3 to allow people to upload images and the image URL is displayed as "https://myapp.s3.amazonaws.com/image/53/98ccfeca46.jpg".
I would like to do one of two things.
I would like to show just
"/image/53/98ccfeca46.jpg"
or 2. Use CName to rename the URL to not show "s3.amazonaws.com" so that the image URL will be
https://myapp.com/image/53/98ccfeca46.jpg
How can I achieve these? Is it possible to cut the image URL right after .com?
I am using <%= link_to xyz.image % > to display the URL.
You could create a helper method for displaying the url, and use string sub to cut off the first part of the url.
So in your helpers folder, in the helper for the corresponding model, you might have a function like
def show_url(url)
return url.sub("https://myapp.s3.amazonaws.com", "")
end
And then in your helper you would have <%= link_to show_url(xyz.image) %>
Or indeed something similar, but using regular expressions
def show_url(url)
return url.match(/.com(.+)/)[1]
end
If you're using carrierwave, you can call .path to retrieve the path of the image. I tried this locally and it worked fine but I'm not sure if it will work in production. Try this:
<%= link_to xyz.image.path %>
Alternatively, you can use CloudFront to server you S3 files, which allows you add a bunch of custom CNAMEs to your distribution, so you can have assets.myapp.com/xyz.jpg

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.

Resources