Ruby on Rails Vacuum Gem - ruby-on-rails

I am new to Ruby on Rails and I am trying to access the Amazon Product API using the Vacuum Gem. Based off this code https://gist.github.com/frankie-loves-jesus/89d24dd88579c7f912f3 I am getting an error at the first line
request = Vacuum.new('GB')
telling me that I have an "Uninitialized constant AmazonAPIController::Vacuum" every example on this gem tells me to use this line of code but I can't understand how to fix the error. It seems to be as simple as putting the code into my controller and showing it.

You didn't add the gem to your Gemfile I guess and didn't run bundle install afterwards. Also you should make sure to restart your Rails server after every change in your Gemfile.
If all that doesn't help, you also could try to just gem install vacuum and then require 'vacuum' in the file you are using (but I would recommend getting it to work on another way).

Related

Controller not recognizing installed Gem

Okay, I know this is a bit of a simple question, but I can't seem to get it to work. I have installed the SmarterCSV gem in my Rails 4 app and am trying to use it in my controller like so:
SmarterCSV.process("/files/csv_file.csv")
I can do this exact process in the rails console for this app, but I cannot seem to get it to work in my controller. Every time I just get the Rails Dead Screen saying uninitialized constant MyController::SmarterCSV. I have tried adding the line
require 'smarter_csv'
But that also breaks to the Rails Dead Screen with the error cannot load such file -- smarter_csv
Any help would be greatly appreciated, Im not entirely sure what I can do...
you can try require 'smarter_csv/smarter_csv' as this is the path of the file in the gem https://github.com/tilo/smarter_csv/blob/master/lib/smarter_csv/smarter_csv.rb
Don't forget to restart your application after bundle install

Rails + Gems (in general): How do gems work?

I've been using Rails for a while and have always used gems in my gemfile, but I never really understood how the functionality of gems that I install actually become available. Say I use the has_permalinks gem (http://haspermalink.org/). It provides a .generate_permalink! method for my Model. Where does this method get defined? How come just I can use this method all of a sudden just by installing the gem? Is there some sort of include/require/load to initialize the gem's code so that it becomes accessible to the rest of the application? Also, where is this code stored when I install the gem?
I answered your questions separately, and out of order, but I think it actually might make it easier to understand the answers in this order.
Also, where is this code stored when I install the gem?
If you're using Bundler, you can do bundle show has_permalink and it will show you where that gem is installed. Here's an example of me doing it with the pg gem:
✗ bundle show pg
/Users/jasonswett/.rvm/gems/ruby-1.9.2-p320#jason/gems/pg-0.11.0
Where does this method get defined?
If you do the bundle show thing, it returns a path - the method is defined somewhere in there. (You can use grep -r 'def generate_permalink' /gem/path to find exactly where if you want.)
How come just I can use this method all of a sudden just by installing
the gem? Is there some sort of include/require/load to initialize the
gem's code so that it becomes accessible to the rest of the
application?
Look at this part of the doc about the Rails initialization process:
http://guides.rubyonrails.org/initialization.html#config-boot-rb
In a standard Rails application, there’s a Gemfile which declares all
dependencies of the application. config/boot.rb sets
ENV["BUNDLE_GEMFILE"] to the location of this file, then requires
Bundler and calls Bundler.setup which adds the dependencies of the
application (including all the Rails parts) to the load path, making
them available for the application to load.
It looks like, fairly early on in the process, Rails looks at your Gemfile and loads all your gems via Bundler. So there's your include.

How to actually use rails-settings?

I'm trying to use rails-settings gem but i'm not sure how to do that.
I've added ledermann-rails-gem gem to Gemfile, runned bundle install, generated a migration based on what's written in wiki, runned rake db:migrate, restarted a server and now, when i'm trying to do anything from rails-settings wiki, i'm getting an error.
Let's say i want to create new variable so i'm putting this code to my controller:
Settings.foo = "bar"
It gives me the following error:
uninitialized constant ApplicationController::Settings
I'm quite 'fresh' in rails development and probably there is some small thing which wasn't mentioned in the wiki because it's obvious (but not for me!). Any help would be appreciated

Creating rails 3 gems: Gems installing successfully, but no functionality

I'm trying to create my first rails gem with jeweller - it's a very simple demo gem with just a "Tester" model and a "Frog" scaffold.
The gem packages up just fine, gem contents "testgem" confirms the desired files are packaged into the gem, and when I "bundle install" it, everything seems to go OK & the gem is in the list of installed gems.
...BUT - I'm not getting any functionality. The activerecord model isn't recognised from the command line (Command "Tester" returns "uninitialized constant Tester"), and the controllers aren't being found either, even when I manually add the resources to config/routes.
This is my first self-built gem, so I could be missing something simple. I've tried placing the necessary files in both [GEM_ROOT]/lib/app/ and [GEM_ROOT]/app/, with gem.path set to [{lib}//, {app}//*].
Any suggestions most appreciated. ;-)
Ach - terribly embarrassing - 30 mins after posting I figured it out.
In case anyone else is wondering: In the gem, I wasn't directing the mygemname.rb file to the engine.rb file. In short, I needed
require 'mygemname/engine' if defined?(Rails)

Necessary steps for using a gem (e.g. json) in Ruby on Rails app?

I'm having trouble getting rubygems to work in my Rails app. Specifically, I'm trying to use the json gem, documented here: http://flori.github.com/json/ I can successfully use JSON.parse() in IRB and script/console but not in my rails app.
I know it's some combination of config.gem 'json' in environment.rb and other things, but can't find a good explanation anywhere.
Can someone give me a concise list of what is required to use this gem OR point me towards comprehensive documentation of using gems in Rails? thanks!
config.gem is used for gem dependency in rails and it does nothing more than telling rails that a gem is needed, and helping the user install the appropriate gem, etc (more details here: http://ryandaigle.com/articles/2008/4/1/what-s-new-in-edge-rails-gem-dependencies)
however by installing a gem, it should be able to be used by the rails app automatically, if not, probably you can add require "json" into environment.rb or in an .rb files in the initializers folder?
Hope it helps =)
I solved it. There's decent documentation here: http://apidock.com/rails/Rails/Configuration/gem
Once you have used config.gem in environment.rb, you should not need to 'require' it later.
My problem was that I had not stopped and restarted the server! It worked in script/console because everything was getting reloaded every time.

Resources