Question

I would like to convert some numbers in words. So I installed the numbers_and_words gem with "gem install numbers_and_words" -

I restarted the server, and tried to run this example from the Read.me in my index.html.erb:

<%= 42.to_words %>

but I get this error:

NoMethodError in Posts#index - undefined method `to_words' for 42:Fixnum

I checked the gem documentation a few times, but I couldn't figure out, what I am missing.

This is my posts controller regarding index.

  def index
    @posts = Post.order("created_at desc")
    @published = Post.where(draft:false).order("created_at desc")
    @drafts = Post.where(draft:true).order("created_at desc")

    respond_to do |format|
      format.html # index.html.erb
      format.json { render json: @posts }
    end
  end

What did I wrong? Did I forget something in the installation process? I am quite new to rails, sorry if this is a trivial newbie question.

Thank you so much for your help! Really appreciated.

Was it helpful?

Solution

Installing a gem is not sufficient to make it available in a Rails project. You need to add it to the Gemfile so that you can manage the dependencies with Bundler.

Edit the 'Gemfile' and add the gem

gem 'numbers_and_words'

Then run bundle again to update the Gemfile.lock

$ bundler

This will make the gem available to the app. It will also autorequire the gem on boot, if the gem uses the standard naming conventions.

It seems this gem is name correctly. Otherwise, you can explicitly require it by setting a require option in the gemfile

gem 'numbers_and_words', require: 'numbers_and_words'

If you just installed the gem locally and you didn't configure the Gemfile, the application will crash once you will deploy it.

OTHER TIPS

Found a solution - not mentioned in the documentation of the gem, but it did the trick for me:

I added

require 'numbers_and_words'

in the rake file. After server restart, it's working.

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