Rails+MongoDB: "No such file to load" - ruby-on-rails

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?

Related

Working on gem within a Rails app

What's the easiest way to work on a plain-ole Ruby gem, and then quickly turn around and run it in a Rails app?
I like to keep application logic out of Rails. Encourages code reuse across projects, and keeps my Rails app clean. It produces, however, an ugly workflow:
Test my gem. rake test
Build my gem. gem build ...
Upload gem to private repository (currently using Gemfury). fury push ...
Update my gem from within Rails (bundle update ...)
Run my Rails code.
Yuck. I'd like to simply save my file in the library, and then watch it work in the Rails project.
Clojure's Leiningen has a concept called "checkouts", allowing you to work on several libraries within one.
Techniques, anyone?
Use Bundler's path directive.
gem 'my_gem', :path => "~/my_gem"
You'll still have to restart the Rails server every time your code changes to reload it, but you won't have to go through the whole build-and-publish step for each revision. Be sure to update your gem reference to something production-worthy before pushing your code.

Automatic identification of Ruby Gems no longer in use within Rails

Is it possible to automatically identify Ruby gems that are no longer in use within a Rails project?
For example if a fellow developer added gem 'nokogiri' to the Gemfile, for a piece of functionally, but the code that depended on that gem has now been removed. I am looking to port my entire project to jRuby so removing the gems that we no longer seems a very sensible starting point.
Thanks
Usually gems are used in a specific way, so for each one you will have to look for patterns manually.
For example, if I had to figure if Nokogiri is being used, I'd use git grep to find Nokogiri occurrences (I assume you use git):
git grep Nokogiri
If nothing is returned, you are probably not using it.
Another way is, if you have a test suite, is just to remove it and see if something breaks. Not foolproof, but if you have good tests it should be a pretty safe path.
On rubyforge there is a gem called gem_unused. It adds an 'unused' command to 'gem'. However, I am not able to test this as it seems to be failing on my system. (I'm new to the rails world so maybe this is an issue someone else could troubleshoot. I filed an issue on it on the maintainer's github)

Reference checklist for starting a new Rails application?

It's easy to create a new Rails application using "rails new myapp" (or start with a template from Rails Wizard or the RailsApps project).
But after that, what do you do? What gems do you always add? What decisions do you make before beginning development? What important items are often overlooked before deploying an app?
I'd like to put together a reference checklist to share. Any suggestions?
For example:
Create a new RVM gemset
Modify the .gitignore file
Switch from TestUnit to RSpec
Set up Guard to run tests in the background during development
Add the viewport metatag to the default application layout
Don't forget to create a sitemap.xml file
Add a Google Analytics snippet
What else?
Starting with a Rails template.
You should look theses resources :
http://railswizard.org/
https://github.com/RailsApps/rails3-application-templates
http://railsapps.github.com/rails-application-templates.html
https://github.com/quickleft/prologue
For me usual process involves:
Add CSS framework (grids, text, forms)
Add Cells
Add Slim (www.slim-lang.com)
Remove Test::Unit for RSpec
Add application config settings (config.yml)
Add Cucumber
Add FactoryGirl
Add Spork
Add Guard (guard-rspec, guard-cucumber, guard-sass, guard-livereload, guard-spork)
Add Git, Github space, + amend .gitignore
Add Heroku (stage + production) spaces
I'll usually copy over my google_analytics helpers and sitemap_controller from other projects during the development process instead of being organised enough to do it from the start. I like to the the testing and deployment options setup from the get go so I can start developing and releasing early and often.
Dave
create rvm gemset, create .rvmrc, modify .gitignore
Then add gems
gem 'pg'
gem 'thin'
gem 'ruby-debug19', :require => 'ruby-debug'
gem 'rspec-rails'
gem 'factory_girl_rails'
gem 'capybara'
then depending on the project, I often use aws3, paperclip, resque, will_paginate and haml (although I try not to use it on new projects anymore)
Most of the time:
Configuration
add .rvmrc
amberbit-config gem (avaible at GH)
modify .gitignore
Views
haml to sass/coffee stack
rdiscount
Tests
rspec instead of unit tests
capybara, factory_gril, spork, timecop
Development
guard-livereload, with guard, libnotify etc.
active_reload for faster development with assets pipeline
annotate if relational db
pry
I almost forgot to mention: mix of html5 boilerplate for rails with twitter bootstrap it's good combo.
The first think that I do is head to http://railswizard.org/ and create a template, before "rails new app".
I always want to set up Factory Girl under /fixtures, and setup
Cucumber along Rspec. Sometimes I use shoulda too.
Initialize the project as a git repository and link it to
github. Set up the app to use PostgreSQL instead of SQLite.
And last I can think of is that I often make an entry, from the
beginning, to load .rb files form /lib automatically.
I don't add anything. Things get added if project requires them.
I don't load up CSS framework just because there's a need for two columns and a rounded button somewhere.
I don't load FactoryGirl because rails test fixtures actually do a fine job as long as you know how to use them. (Hint: you don't need 100 instances of User in your fixtures)
I don't load RSpec/Cucumber/etc because UnitTest is just as good and I prefer keeping things simple.
There's absolutely no reason to bloat project with things just because you "might need it"
I got tired of having to remember and repeat the mundane tasks required for every new app.
If you're looking for guidance on getting started, we've recently (yesterday!) released a tool to speed up the process, with all sorts of tried and tested libraries for the various aspects of a Rails web app, as well as cleanup scripts to get the fresh app just the way it should be.
Check out http://drone.bz to build an app the same way we do. Under the hood, it uses the app_drone gem to resolve dependencies and build the actual template, but the UI is probably all you need to get started.
There are several similar tools out there, but I decided to be highly opinionated in my recipe choices, and the end result is a solid foundation on which to start developing :)
P.S. Even if you don't use the drones, you can take a look at the steps that are common enough in Rails dev to be automated.

Problem creating Rails 3 Engine

I'm working on creating my first Rails 3 engine and I'm currently getting the following error message
is a Railtie/Engine and cannot be installed as plugin (RuntimeError)
To give you a bit more background I'm not doing the Engine as a gem, but I've place the Engine files in the vendor/plugins folder. I know there is an issue with loading when in the plugins folder, but I'm not clear on how to solve the problem.
My rails/init.rb file in the Engine has the following code
require 'rails'
module RailsApp
module MyEngine
class Engine < Rails::Engine
config.after_initialize do
RailsApp::GameType.register do |game_type|
game_type.name = "TreasureIsland"
game_type.version = "0.1"
game_type.thumbnail = "teasure_island.jpg"
end
end
end
end
end
Suggestions?
I think I remember reading that Railties would not work in plugins directory, because they are intended to be loaded at a different point in the application boot process. I would recommend creating a gem using something like Jeweler, which does alot of the work for you. The purpose of the Railtie/Engine is to have a reusable component that can be included in multiple rails apps. Once you have a gem created, you can point to the local gem path within your Gemfile. This allows you to see changes in your engine code inside your rails app without having to build and reinstall the gem every time you make a change to the engine code. Of course you would want to point bundler to the installed gem in production. I would recommend putting it on github and using that URL in your Gemfile in production.
Bundler local gem example:
#Gemfile
gem "my_engine", :require => "my_engine", :path => "/my_engines/my_engine"
Check out the Modern Rubyist's website. He has a good series on creating Railties and Engines. There may have been some changes to Rails since this was written, but I think most of it is still relevant. It helped me out a good bit when I was learning how to write Engines with Rails 3.
http://www.themodestrubyist.com/2010/03/01/rails-3-plugins---part-1---the-big-picture/
http://www.themodestrubyist.com/2010/03/05/rails-3-plugins---part-2---writing-an-engine/
http://www.themodestrubyist.com/2010/03/16/rails-3-plugins---part-3---rake-tasks-generators-initializers-oh-my/
http://www.themodestrubyist.com/2010/03/22/rails-3-plugins---part-4---more-on-generators/
John, I believe engines (which are typically gems) vs plugins (which live in vendor) are loaded at different points in the rails initialization process.
Engines actually have a bit more flexibility, they can hook deeper into rails. In addition, packaging as a gem has a lot of advantages: easier to share across apps, easier to maintain in a separate code repo, easier version control.
I'm creating my first rails engine right now and created a useful starting point and walk-through for getting started:
http://keithschacht.com/creating-a-rails-3-engine-plugin-gem/

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)

Resources