In my rails3 engine gemfile, I added:
gem 'paperclip'
gem 'inherited_resources'
I also added this to my gemspec:
s.add_dependency "paperclip"
s.add_dependency "inherited_resources"
I then ran bundle install for both my engine and the client app and started up the console for the client app in order to first test paperclip, to which I get:
Using /Users/ynkr/.rvm/gems/ruby-1.9.2-p180 with gemset rails31beta
ynkr % rails c
/Users/ynkr/.rvm/gems/ruby-1.9.2-p180#rails31beta/gems/actionpack-3.1.0.rc4/lib/action_dispatch/http/mime_type.rb:101: warning: already initialized constant YML
Loading development environment (Rails 3.1.0.rc4)
ruby-1.9.2-p180 :001 > b=Blog::Blog.first
Blog::Blog Load (0.2ms) SELECT `blogs`.* FROM `blogs` LIMIT 1
=> #<Blog::Blog id: 1, user_id: 1, context_id: 2, title: "Cmd Line Blog Title", title_for_url: "cmd-line-blog-title", teaser: "This is the teaser for the command line blog", content: "Some content for the cmd line blog", created_at: "2011-06-28 06:06:55", updated_at: "2011-06-28 06:06:55">
ruby-1.9.2-p180 :002 > b.photos
NoMethodError: undefined method `has_attached_file' for #<Class:0x00000102a57fc8>
from /Users/ynkr/.rvm/gems/ruby-1.9.2-p180#rails31beta/gems/activerecord-3.1.0.rc4/lib/active_record/base.rb:1078:in `method_missing'
from /websites/gems/blog/app/models/blog/photo.rb:6:in `<class:Photo>'
from /websites/gems/blog/app/models/blog/photo.rb:1:in `<top (required)>'
from /Users/ynkr/.rvm/gems/ruby-1.9.2-p180#rails31beta/gems/activesupport-3.1.0.rc4/lib/active_support/dependencies.rb:452:in `load'
OK, so it appears the has_attached_file method from the paperclip gem cannot be found. Why? I'm not sure.
Moving on to inherited_resources, I altered a scaffold generated controller to look like the following:
class Blog::ContextsController < InheritedResources::Base
before_filter :redirect_unless_admin
end
and load up the index page for that to which I am greeted (in the logs) with:
Started GET "/blog/contexts" for 127.0.0.1 at 2011-06-28 12:06:25 -0700
ActionController::RoutingError (uninitialized constant InheritedResources):
Rendered /Users/ynkr/.rvm/gems/ruby-1.9.2-p180#rails31beta/gems/actionpack-3.1.0.rc4/lib/action_dispatch/middleware/templates/rescues/routing_error.erb within rescues/layout (0.5ms)
My question is why are these not working? My hunch is that there is something about engines, namespacing and loading up gems that I am totally unaware of.
You need to require each gem within your engine in an initializer file, eg. lib/<your_engine_name>/engine.rb
Bit more of an explanation over here...
Rails Engine - Gems dependencies, how to load them into the application?
Related
When I look for a problem, for example with a specific ActiveRecord object, I often find myself doing the following on my production system:
# RAILS_ENV=production bundle exec
irb(main)> article = Article.find(123)
=> #<Article id: 123, title: "Foobar">
irb(main)> article.do_something(3)
NoMethodError: undefined method `id' for nil:NilClass
Sometimes I can't reproduce, why the line article.do_something(3) throws an error, so I want to debug it directly on my server, in production mode.
The problem now is: How do I step into the method #do_something with the argument 3 on the object / instance article?
Of course, one could set a breakpoint in that method, reload production and let all their customers wait on that breakpoint till I'm done debugging... But that wouldn't be the best idea.
So, is there a way to debug into a method of a specific instance from a running irb / pry session? (both would be ok)
After trying around and more googling, I think I found a solution that works for me.
Log in to your rails console / irb / pry session
Setup your case (e.g. load your models, require dependencies...), so that you can execute the code you want to debug in one line
require 'byebug' (or require 'debugger' for older ruby versions)
Now the interesting part: Put a debugger statement in front of the line you want to debug like this binding.pry; user.do_something or debugger; user.do_something
Now you are in your debugger. Maybe you have to jump to the next line with next (or just n if you have shortcuts enabled) to step into your method.
Here is a complete example from our production system:
[1] pry(main)> require 'byebug'
=> true
[2] pry(main)> user = User.find(2)
User Load (0.3ms) SELECT `users`.* FROM `users` WHERE `users`.`id` = 2 LIMIT 1
=> #<User id: 2, name="XXX XXX">
[3] pry(main)> user.full_name
NameError: undefined local variable or method address for #<User:0x0055b168663590>
[4] pry(main)> binding.pry; user.full_name
[68, 73] in /usr/src/app/app/models/user.rb
68: end
69:
70: def full_name
=> 71: "#{address.firstname} #{address.last_name}"
72: end
73: end
(byebug)
sure pry, byebug, etc. are awesome BUT you don't need a gem
Ruby 3 (and newer versions of Ruby 2.x) has irb debugging built in
require 'irb'
def run
a = "hello"
binding.irb # debug starts here, eg puts a
a = "something else"
a
end
run
You can use pry-debugger (through ruby 1.9.3) or pry-byebug (Ruby >= 2).
Both of these allow you to set breakpoints allowing you to interactively step through your code as it runs, inspecting variable values, method return values, etc.
How you'll manage this with production data I'm not exactly sure, but this will allow you to debug a particular method.
I am using the Matrix library in Ruby, and when I use it in irb, it works fine:
2.1.5 :001 > require 'matrix'
=> true
2.1.5 :002 > a=Matrix.build(3,3) { |row,col| row==col ? 0 : 1}
=> Matrix[[0, 1, 1], [1, 0, 1], [1, 1, 0]]
But when I use it from Gemfile, the behavior is different:
Loading development environment (Rails 4.1.8)
2.1.5 :001 > a=Matrix.build(3,3) { |row,col| row==col ? 0 : 1 }
NoMethodError: undefined method `build' for Matrix:Module
I inspected this a bit, and I noticed the require behavior loads a class, while Gemfile loads a module:
Require:
2.1.5 :003 > Matrix.class
=> Class
2.1.5 :004 > Matrix.constants
=> [:EigenvalueDecomposition, :LUPDecomposition, :SELECTORS, :ConversionHelper, :CoercionHelper, :Scalar, :ErrDimensionMismatch, :ErrNotRegular, :ErrOperationNotDefined, :ErrOperationNotImplemented]
Gemfile:
2.1.5 :002 > Matrix.class
=> Module
2.1.5 :003 > Matrix.constants
=> [:VERSION]
How should I include this in my Rails project? I can use require somewhere, but I'd really rather load it as a dependency from Gemfile.
Ruby Gemfile gem has different behavior than 'require' gem
Yes, that's because they are two different things. Adding a gem to your Gemfile does not replace the need to require classes, it only informs the bundler that this gem is needed to run your program. See here for more information about Gemfile and Bundler.
I think you're installing the matrix 'gem' http://rubygems.org/gems/matrix in your gemfile. The matrix class is part of stdlib so you should not need to install a gem for it: http://www.ruby-doc.org/stdlib-2.0/libdoc/matrix/rdoc/Matrix.html. You do need to require it to use it however.
TL/DR: Remove gem 'matrix' from your Gemfile and just use require 'matrix' to use the stdlib matrix implementation rather than the seemingly empty 'matrix' gem Module.
Out of curiosity, where is Rails.application defined? If I do this in irb in a Rails-powered app, I got an error:
$ irb
1.9.3-p327 :001 > require 'rails/application'
=> true
1.9.3-p327 :002 > class App < Rails::Application; end
NoMethodError: undefined method `application' for Rails:Module
I have tried requiring more, like active_support and rails/railtie/configuration but no luck.
The purpose of this is to load a minimal Rails env where I can test an ActiveRecord::Base helper :)
Usually when working with Rails you use rails console instead of IRB. When you run rails console it will boot up your Rails application.
FWIW, Rails.application is defined in railties:
lib/rails.rb
2.0.0-p247 :006 > load './app/models/user.rb'
NameError: uninitialized constant ActiveRecord
from /home/action/iAuth/app/models/user.rb:1:in `<top (required)>'
from (irb):6:in `load'
from (irb):6
from /home/action/.rvm/rubies/ruby-2.0.0-p247/bin/irb:13:in `<main>'
Below is my User Model.
class User < ActiveRecord::Base
end
When i try to load my user.rb in my irb, i get the above error.
start your irb session with
rails console
and not:
irb
rails console would load your rails environment and your model for you, so you can do things like:
User.all or User.new without loading the class as it has been preloaded by rails console already
there's another case.
if you are using mongoid ( mongo db adapter) , and you have to make sure in config/application.rb, you must require activerecord:
require "active_model/railtie"
I would like to know if when using pry is possible to have access to the variable app?
As an example, when I try to access the root_path I get the following error:
[14] pry(main)> app.root_path
NameError: undefined local variable or method `app' for main:Object
Someone said that "It does now work with pry and 3.2.9". I am using rails 3.2.12, but it doesn't seem to work.
I have gem 'pry' in my GemFile group development and in config/environments/development.rb the following
# Use Pry instead of IRB
silence_warnings do
begin
require 'pry'
IRB = Pry
rescue LoadError
end
end
Yes, it works
➜ MyApp git:(master) rc
Loading development environment (Rails 3.2.13)
[1] pry(main)> app.root_path
=> "/"
I use pry-rails in favor of your overriding of IRB in an initializer.
group :development do
gem 'pry-rails'
end
https://github.com/rweng/pry-rails
Though this is solved, if you don't want to or can't use the 'pry-rails' gem for some reason you can also add the following into your .pryrc file:
if defined?(Rails) && Rails.env
extend Rails::ConsoleMethods
end
You can read more in the pry wiki