Truncate string in Rails: ... showing up on strings at length
truncate
is the right method. This might be a bug in your version of rails? Here's what I get on my console:
[5] pry(main)> helper.truncate("This post is exactly 56 characters characters characters characte", length: 65)
=> "This post is exactly 56 characters characters characters characte"
[6] pry(main)> helper.truncate("This post is exactly 56 characters characters characters characte", length: 64)
=> "This post is exactly 56 characters characters characters char..."
I'm running Rails 4.0.4 in this example.
Truncate a string without cut in the middle of a word in rails
If you pass in a separator to the truncate method it will perform a natural word break instead of truncating at a middle of a word
Something like this should work (vary the length to whatever you want to remove it altogether if you want the default of 30 characters):
truncate("Praesent commodo cursus magna, vel scelerisque nisl consectetur et. Aenean eu leo quam. Pellentesque ornare sem lacinia quam venenatis vestibulum.", :length => 17, :separator => ' ')
More information about the options you can have in truncate can be found in the Documentation
truncate text after exceeding certain size
argument of truncate
means size of output string (with "..."):
str.truncate(13)
=> "abcdefhijk..."
str.truncate(13).size
=> 13
You can change default omission(...
) by empty space if you want:
str.truncate(13, omission: '')
=> "abcdefhijklmn"
More about Rails String#truncate
here.
Ruby on Rails - Truncate to a specific string
I would use just simply truncate, it has all of the options you need.
truncate("And they found that many people were sleeping better.", :length => 25, :omission => '... (continued)')
# => "And they f... (continued)"
Update
After sawing the comments, and digging a bit the documentation it seems that the :separator
does the work.
From the doc:
Pass a :separator to truncate text at a natural break.
For referenece see the docs
truncate(post.rendered_body, :separator => '[---MORE---]')
On the show page you have to use gsub
Rails truncate strings in an array
Try this:
%w(example test product listing).map { |w| w.size > 5 ? w[0,5] + "..." : w }
Output:
[examp..., test, produ..., listi...]
So in your code you could implement that like this (untested):
# Helper method
def truncate_tag(tag)
tag.size > 5 ? tag[0,5] + "..." : tag
end
# View
<%= @medium.marker_list.map { |m|
link_to truncate_tag(m.titleize),
:controller => "media",
:action => "index",
:media => m
}.join(' ').html_safe %>
How can I center truncate a string?
Here is a modified version of Mike Woodhouse's answer. It takes 2 optional params: a minimum length for the the string to be ellipsisized and the edge length.
class String
def ellipsisize(minimum_length=4,edge_length=3)
return self if self.length < minimum_length or self.length <= edge_length*2
edge = '.'*edge_length
mid_length = self.length - edge_length*2
gsub(/(#{edge}).{#{mid_length},}(#{edge})/, '\1...\2')
end
end
"abc".ellipsisize #=> "abc"
"abcdefghi".ellipsisize #=> "abcdefghi"
"abcdefghij".ellipsisize #=> "abc...hij"
"abcdefghij".ellipsisize(4,4) #=> "abcd...ghij"
"Testing all paramas and checking them!".ellipsisize(6,5) #=> "Testi...them!"
truncate a specific string in a view
It looks like you have h2
as a variable in the first parameter of truncate
, I'm guessing you want @user.portfolio.html_safe
or similar?
Edit: It should probably be link_to truncate(@user.portfolio_title.html_safe, :length => 17), "#{@user.portfolio}", :target => "_blank"
Related Topics
How to Delete Specific Characters from a String in Ruby
How to Run a Ruby Script Within Bundler Context
How to Read the Body Text of an Email Using Ruby's Net/Imap Library
Strange Activerecord::Associationtypemismatch
How to Use Ruby Minitest::Spec with Rails for API Integration Tests
Installing Ruby 2.0.0 Using Rvm
How to Override Static Class Method Using Module in Ruby
Rails Activeadmin: Showing Table of a Related Resource in the Same View
How to Fire and Forget a Subprocess
Ruby: How to Make a Public Static Method
How to Set a Blank Value for an F.Select Form Field