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

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)

Related

Ruby on Rails Vacuum Gem

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).

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

Ruby on Rails: How do I use a Gem? (Vacuum Amazon API)

I found an Amazon Gem that I want to use https://github.com/hakanensari/vacuum/.
I've only ever used gems with in depth tutorials or following a RailsCast. I'm looking for tips on using gems I find online. I'll explain the steps I'm doing, and hopefully someone can give me some ideas on what else I should be doing when using a new gem. Also, if you have a good tutorial or explanation on gems, that would be great too.
I started off examining the Gem on Github, I'll point out the things I took notice of. Let me know if there are things I'm missing I should notice.
Examining Gem on Github
Go to the examples and look at "examples/product_advertising/basic_lookup.rb"
Follow the required file, and checkout "examples/product_advertising/shared.rb"
Notice, I need to install "pry"
Notice, the examples expand on the "lib" folder
Check out "credentials.yml"
Notice a "#req" is instantiated as a new Vacuum object.
Then back in basic_lookup.rb, it looks like it's assigning the lookup value, then binding the response to some kind of "pry" view.
Next, I'll try implementing these examples in my own project. Here's where I'm not sure what to do as far, as files are concerned.
Attempt Implementing Example
Install vacuum gem
gem install vacuum
Install pry gem
gem install pry
Added "shared.rb" and "credentials.yml" to my "app/controllers" directory
Replaced the information in "credentials.yml" with my information
Attempt to copy the information from "basic_lookup.rb" into an existing controller
def amazon
require File.expand_path('../shared.rb', __FILE__)
res = #req.look_up '0816614024'
items = res.find 'Item'
binding.pry
end
Create a route
match '/test' => 'products#amazon'
Go to test page and receive the following error
undefined method 'look_up' for nil:NilClass
I would like to point out, at this point I haven't added the lib folder.
Questions
I like the credentials.yml is separated out, when I want to add this to my project, where should I save that file?
I like the shared.rb file, should I just put that in the controller folder?
Why is it referencing the "lib" folder in "shared.rb"? Do I need to copy that directory into my project?
I appreciate you sticking around and reading all of this. I'm still trying to get a handle on using a gems, so any help or tips are great. Really, I'm just trying to figure out, how do I find any gem and start using it appropriately.
Thanks for any help you can give me!
I like the shared.rb file, should I just put that in the controller folder?
Answer = yes, you just put that file in controller folder.

Rails+MongoDB: "No such file to load"

New to MongoDB and trying to get a second test project working. I emphasize second because I'm wondering if that may be part of the problem as in /data/db/ i still have the files for the first proof-of-concept MongoDB <- MongoID -> Rails project that I got up and running without a problem a few weeks ago.
Now I'm experimenting with building a new Rails project from scratch with some flags (-T -O -J) and then running the associated install/config generators: mongoid:config, rspec:install, jquery:install.
So far so good... until I generate my first scaffold and then try to access it and get the response:
"No such file to load -- vendor"
now, I'm not too surprised as there's no new xxx_development.x files in /data/db/. But I don't remember creating them the first time and tutorials I've been using don't mention such a step? Early onset senility? Or is something amiss? Perhaps MongoDB (or MongoID) can only handle one MongoDB per db directory?!?
#Gemfile
source 'http://rubygems.org'
gem 'rails', '3.0.4'
gem 'mongoid', '2.0.0.rc.7'
gem 'bson_ext'
...
Using default mongoid.yml.
I've searched S.O. for related problems and there were many but most either have problems loading a gem, which is not my case, or state that the bson_ext version must match the mongoid version.. but most of these cases seem old and now I don't even think that's possible with mongoid approaching 2.0 and bson_ext still at 1.2.
Figured it out by building another test app from scratch. there seems to be a bug (or my lack of understanding) somewhere perhaps involved with the -J flag to rails new (or with the combo of using the -J, -T & -O flags at same time?). Anyway, it uncomments the following line in application.rb:
config.action_view.javascript_expansions[:defaults] = %w()
which should read:
config.action_view.javascript_expansions[:defaults] = %w(jquery rails)
adding "jquery rails" made the app work... not sure what the resource name had to do with the original error message!?! Red Herring?

Installing and using acts-as-taggable-on

This is going to be a really dumb question, I just know it, but I'm going to ask anyways because it's driving me crazy.
How do I get acts-as-taggable-on to work?
I installed it as a gem with gem install acts-as-taggable-on because I can't ever seem to get installing plugins to work, but that's a whole other batch of questions that are all probably really dumb. Anyways, no problems there, it installed correctly.
I did ruby script/generate acts_as_taggable_on_migration and rake db:migrate, again no problems.
I added acts_as_taggable to the model I want to use tags with, started up the server and then loaded the index for the model just to see if what I've got so far is working and got the following error: undefined local variable or method `acts_as_taggable' for #.
I figure that just means I need to do something like require 'acts-as-taggable-on' to my model's file because that's typically what's necessary for gems. So I did that hit refresh and got uninitialized constant ActiveRecord::VERSION. I'm not even going to pretend to begin to know what that means went wrong.
Did I go wrong somewhere or there something else I need to do. The installation instructions seem to me like they just assume you generally know what you're doing and don't even begin to explain what to do when things go wrong.
Did you try to define your gem dependencies in config/environment.rb (Rails 2.3):
Rails::Initializer.run do |config|
#...
config.gem 'acts-as-taggable-on'
#...
end
Or in Gemfile for Rails 3 or if you use already Bundler with rails 2.3:
gem 'acts-as-taggable-on'
This should make the require 'acts-as-taggable-on' unnecessary
Maybe following the installation here can help.
For example you don't need to:
require 'acts-as-taggable-on'
but:
class User < ActiveRecord::Base
acts_as_taggable
end
Otherwise you need to post more details about the error.
I installed acts-as-taggable-on for my app through github. If you want to try that method instead of the gem, you can read my this post that explains my experience: http://blog.mediummassage.com/2010/04/27/creating-categories-in-the-store-with-tags/

Resources