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.
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
Then run bundle again to update the
Gemfile.lock
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 gemfileIf you just installed the gem locally and you didn't configure the Gemfile, the application will crash once you will deploy it.