I need to perform this in the rails console, following the instructions at Rails API ActionView::Helpers::TextHelper I already found a solution of my problem at the following Stackoverflow Discussion, but I though the question could be useful to be posted. If you want me to delete it, just tell me.
I wanted to create an ActionView::Helpers::TextHelper
in my rails console
texthelper = ActionView::Helpers::TextHelper
=> ActionView::Helpers::TextHelper
I create a string
string = "I want to call the truncate method"
=> "I want to call the truncate method"
I want to call the truncate method, in my console on this string.
truncate(string, length: 25, omission: '... (continued)')
=> "And they f... (continued)"
When I input in the console:
truncate("Once upon a time in a world far far away", length: 17, separator: ' ')
I receive
undefined method `truncate' for main:Object
The whole idea come from this post.. Undefined method `truncate' in model
I found the answer at the following discussion Undefined method `truncate' in model
I should have included the ActionView::Helpers::TextHelper in my rails console with the following command
include ActionView::Helpers::TextHelper
Then I could call the truncate method.
truncate(string, length: 17, separator: ' ')
=> "this is a tesf..."
My understanding now is that with the include
command, the methods from ActionView::Helpers::TextHelper
are now available in the console. truncate
method can be executed on text
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With