I have come across an issue with my gem, it seems that it is not initializing when starting the rails server, if i run this script in the console i can access the model classes, however when i start the rails server i get 'uninitialized constant errors'
require "blogModels/version"
module BlogModels
Gem.find_files("models/*.rb").each do |f|
filename = File.basename(f, '.*')
class_name_symbol = filename.classify.to_sym
autoload class_name_symbol, "models/#{filename}"
end
end
my gemfile
gem 'mygem', :git => "pathtogem"
afterwhich i ran bundle
Would there be a reason as to why it is not loading when i am retrieving it from git?
Thanks
Related
I am using gem differ https://github.com/pvande/differ
I have a helper
require 'differ'
module AnswersHelper
def self.getDiff (text1, text2)
Differ.format = :html
diff = Differ.diff_by_word(#current, #original)
end
end
But I get an error No such file to load -- differ
If I remove require line
I get an error at that line
Differ.format = :html
uninitialized constant QuestionsController::Differ
When I tried following commands in rails console it worked
require 'differ'
diff = Differ.diff_by_word("text1","text2)
I have gem differ in my gemfile
and also I tried
require_relative 'differ'
and
require './differ'
UPD: seems restarting server helps, I'll check it right now
Restarting server helped........
We are upgrading Rails 3.2 engine to Rails 4.2.0 on ruby 2.0.0. Gem 'activerecord-session_store' was added to engine's gemspec following gem's instruction:
s.add_dependency 'activerecord-session_store'
and added following in initializers/session_store.rb under dummy:
Dummy::Application.config.session_store :active_record_store, :key => '_my_app_session'
then, we did bundle install. When we ran:
bundle exec rails generate active_record:session_migration
There is the error from the gem's generator:
/activerecord-session_store-0.1.1/lib/generators/active_record/session_migration_generator.rb:16:in `session_table_name': uninitialized co
nstant ActiveRecord::SessionStore (NameError).
We move the gem into engine's Gemfile and same error. Why the SessionStore is still not initialized?
Update
In engine's engine.rb under lib, the session table is pointed to:
initializer "Authentify.add_middleware" do |app|
ActiveRecord::SessionStore::Session.table_name = 'authentify_sessions'
app.middleware.use ActiveRecord::SessionStore
end
The setup works for Rails 3.2.
If you were not using the default table name for sessions, set:
ActiveRecord::SessionStore::Session.table_name = 'your_old_session_table'
in config/application.rb.
Additional configuration.
What we did is to add gem 'activerecord-session_store' to engine's Gemfile in addition to the .gemspec. The error disappeared.
I have a gem that I am making and it is located at the same level as a rails app.
I am trying to use the gem in the rails app, but am not able to make it work - the constant I am trying to use, which is defined in the gem, is not accessible to the rails app for some reason.
What am I doing wrong here? What steps can I take to begin debugging the cause of the problem?
In the rails console, $:.grep /mygem/ shows me ["/Users/zabba/mygem/lib"]
Directory structure, with the contents of certain files:
~/mygem/
lib/
mygem/
some_class.rb
module Mygem
class SomeClass
end
end
mygem.rb
require 'mygem/some_class'
~/railsapp/
Gemfile:
gem 'mygem', path: '../mygem', require: 'mygem'
app/
models/
a_model.rb:
# require 'mygem' cannot find file
class AModel
def hello_world
SomeClass.new # Cannot find constant
Mygem::SomeClass.new # Cannot find constant
end
end
Can you create a folder vendor/gems and copy your gem in there. Then in you Gemfile
Gemfile
gem 'mygem', :path => "vendor/gems/mygem-0.0.1"
Then bundle install
I have ruby and my gems installed in my home directory using rvm. 'require RMagick' works from the console and irb, but not from within my Rails app. I have the gem listed in my Gemfile.
require 'RMagick'
class PetsController < ApplicationController
def image
image = Image.new(image_path("base/Base.png"))
#image = Pet.composite(0, '4a4a4a')
#response.headers["Content-type"] = image.mime_type
render :text => image.to_blob
end
end
If I remove the require from my controller, I get
uninitialized constant PetsController::Image
I also have tried require 'RMagick' in boot.rb, and by referring to the Image object using Magick::Image (which gives me uninitialized constant PetsController::Magick.
What am I doing wrong?
Solved it! Restarting the server fixed it. I guess I hadn't restarted the server when I added rmagick to my gemfile.
Is it possible that you are running the rails app with another user than the one that has the RMagick gem installed?
I have a very simple controller:
require 'net/ssh'
class MyController < ApplicationController
def foo
render :text => 'bar'
end
end
But when I request http://server:3000/my/foo I get:
MissingSourceFile in MyController#foo
no such file to load -- net/ssh
The gem is installed
> gem list net-ssh
*** LOCAL GEMS ***
net-ssh (2.0.11)
Also, I tried require 'net/ssh' in IRB, and it works.
MyController works fine on Windows, but fail on Ubuntu.
What can be wrong?
In a project I am working on we have used the config/environment.rb file to hold the gem require stuff. So
Rails::Initializer.run do |config|
# ...
config.gem 'net-ssh'
config.gem 'daemons'
config.gem 'slave'
config.gem 'vpim'
config.gem 'json'
# ...
end
I think you will require 'net-ssh' rather than 'net/ssh'. However we did run into a problem where have a hyphen in the name of the gem led to failures. Then we had to do
config.gem 'Ruby-IRC', :lib => 'IRC'
so that version maybe required for you. So that would be
config.gem 'net-ssh', :lib => 'net/ssh'
in case of rails 3.0
this solution if OK.
add this in the yourapp/Gemfile,
gem 'net-ssh
This may help:
Rails Gem Dependencies and Plugin Errors
This is also worth watching:
Railscasts: Gem Dependencies
In my case, since it's a stand alone ruby app, I only needed to require rubygems.
You can also use Dr Nic's ''gemsonrails'' and load vendored gems as plugins, check:
http://gemsonrails.rubyforge.org
I think, the original problem was that I used normal user instead of root:
$ gem install net-ssh
WARNING: Installing to ~/.gem since /usr/lib/ruby/gems/1.8 and
/usr/bin aren't both writable.
WARNING: You don't have /home/alex/.gem/ruby/1.8/bin in your PATH,
gem executables will not run.
So, I guess, rails could not find this gem.