Limiting Characters/Words in View - Ruby on Rails

Limiting characters/words in view - ruby on rails

Try the truncate view helper

<%=h truncate(comment.body, :length => 80) %>

Is It Possible To Truncate (or limit the amount of words displayed) On A Rendered Post in Ruby on Rails?

I can't put the truncate in the _post partial because then it will truncate it when the specific post is clicked on, which is not what I want.

Naturally. This then calls for one of two solutions.

  1. Truncate text of post objects prior to rendering, or otherwise make the post partial see shortened text (for example, use a Presenter pattern)

    <% for post in posts %>
    <% post.title = truncate(post.title) %> // truncate
    <%= render post %> // then render
  2. Use another partial for rendering posts on the index page, the one with truncate inside.

Ruby on Rails: How can i take/cut first 300 words or characters from a string?

str = "many words here words words words ..."
first_500_words = str.split(" ").first(500).join(" ")
first_500_chars = str[0..500]

One liner to limit Array of Words to less than N number of characters in Ruby?

Too bad there is a fencepost condition, otherwise this would be a much cleaner code with the take_while method

def chop(array, separator = "", max_size = 40)
so_far = 0
sep = separator.size
array.take_while{|word| (so_far += word.size + sep) <= max_size + sep }
end

It checks it is less or equals than max_size + sep to compensate we are adding a separator to every word, even though the last one doesn't need it.

Building a blog in rails -- how do I limit text and put a read more link to show the rest of post?

To show a certain number of characters, you can use truncate helper method to truncate your article.

truncate("Once upon a time in a world far far away")
# => "Once upon a time in a world..."

If you also have question about "read more" link, please read "resource routing" section in Rails Routing from the Outside In. You should show all your posts in index action (probably with pagination), and show single post in show index. Truncate the post in the index view, and show the full post in show view.

Set maximum length in Text field in RoR

Here is how you can do it:

<%= text_field_tag 'create_text', nil, :maxlength => 15, :size => 40 %>

Source: http://api.rubyonrails.org/classes/ActionView/Helpers/FormTagHelper.html#method-i-text_field_tag

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.

Limiting the number of characters per line in css

If I understood you properly, you can use word-wrap property like this:

.example-wrapper {
background-color: red;
max-width: 100px;
word-wrap: break-word;
}

.example {
max-width: 100px;
white-space: normal;
}

The word-wrap property allows long words to be able to be broken and wrap onto the next line.



Related Topics



Leave a reply



Submit