Rendering field data as a link in Ruby on Rails - 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.

Related

Rails select opton using images

i have a select_tag in my rails form where i want to show images as option, i tried searching for a solution online but there is nothing quite good.
my form
<%= form.select :transport, ['Select an image', 'http://pngimg.com/uploads/tesla_car/tesla_car_PNG24.png', 'http://pluspng.com/img-png/png-hd-bike-ktm-duke-bike-png-download-1600.png' %>
i want something like this:
click here
Instead of struggling to let Rails handle it, It's recommended to use JQuery to populate options in the drop-down list.
This link guides you through the process to do it.
Click Here

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.

Using Scaffold Form to Embed SoundCloud Link

I'm trying to create a form in an app that takes a song's title, artist and soundcloud embed link so that these can be used in a post.
I created a scaffold, and set soundcloudembed:text
I added a link <%= post.soundcloudembed %> at the position where I want the embedded player to be displayed. This link does not work.
Does anyone know of a way to fix this?
Since your soundcloadembed field contains html tags you should tell rails to output raw data using one of these two methods:
<%== post.soundcloudembed %>
<%= post.soundcloudembed.html_safe %>

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.

Rails 3.0 . How to pass the select box value through link_to

I knew that Rails3 no longer supporting link_to_remote.... instead of that we can modify
the link_to method by using different syntax as follows :
link_to "Send",{:action =>"send_mail",:question_id =>question.id},:remote => true,:class =>"button small"
My problem is, in my view i keep the select box which contains the list of user's name near by send link button (which user the above syntax)... i want to pass the selection box value to link_to when user click the send button
Here is my View code :
"send_mail",:question_id =>question.id,:user_value
=>encodeURIComponent($F('user_id'))},:remote => true,:class =>"button small" %>
I don't know how to achieve this ....can any one please suggest me on this.
edit: ok now is see your last comment... never mind
What I got from the question, you are looking for something like this:
http://marcgrabanski.com/articles/jquery-select-list-values
It's not really a Rails problem since you can't change the Ruby code from within the rendered HTML (at least that would be very risky if it's possible). The code from above can be easily changed to your needs so that the user gets redirected to the URL that matches the button value.

Resources