Question

How should I render a long URL in a table, I would prefer not to show the entire URL since it might exceed the table length. I would want to shorten and show it, if it exceeds a certain predefined size (say 50 chars). If I click on it, it should take me to a new page where I can view the contents of the link. How do I implement this?

Was it helpful?

Solution

Rails has a helper for that

link_to truncate("And they found that many people were sleeping better.", :length => 25, :omission => '... (continued)'), your_path
# => "And they f... (continued)"

OTHER TIPS

I'm assuming table means the html kind, if the long url string is in a instance named url, do

<td><%= link_to url[0...49], some_path %></td>

should give you the first 50 characters.

For extra to add that ... thing if it exceeds,

<td><%= link_to url.length < 50 ? aa : "#{aa[0..49]...}", some_path %></td>

haven't tested the code, sorry if there's a syntax error.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top