Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails: Undefined method `truncate' for main:Object

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.

  1. I wanted to create an ActionView::Helpers::TextHelper in my rails console

    texthelper = ActionView::Helpers::TextHelper
    => ActionView::Helpers::TextHelper
    
  2. I create a string

    string = "I want to call the truncate method"
    => "I want to call the truncate method"
    
  3. 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

like image 826
Fabrizio Bertoglio Avatar asked Dec 23 '22 19:12

Fabrizio Bertoglio


1 Answers

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.

like image 136
Fabrizio Bertoglio Avatar answered Jan 07 '23 23:01

Fabrizio Bertoglio