How to add link field in migration? - ruby-on-rails

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.

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 link_to a particular part of a page using a class Ruby on Rails?

I have a navigation and for certain parts, I'd like to link_to a particular sections of the page when it is navigated to it.
I tried this:
<li><%= link_to "Submit Data", about_path(anchor: "submit-data") %>
but I ended up on the correct about page but not to the section I desired. The submit-data div is lower on the about page. Is this more complex than I think it is? Please send examples.
Try
<%= link_to "Submit Data", "#{about_path}#The_SECTION_ID_YOU_WANT_TO_TRAVERSE_TO" %>
I fixed this by changing submit-data to an id instead of a class. The above link_to worked when I did this action.
Typically anchors (the #my_anchor_name part of a URL) cause the scroll position of the page to move to a corresponding anchor target in the markup.
The anchor targets are written as:
<a name="my_anchor_name"></a>
You can put headers inside if you want as well, for clean formatting:
<a name="my_anchor_name"><h1>Submit Your Data Here</h1></a>

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.

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.

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.

Resources