I have a rails project which uses mongo db, and I wrote an mountable engine named 'report_service'.
I used it like this in main rails project:
gem 'report_service', :git => 'git#xx.com:report_service.git', :branch => :master, :require => false
I don't want this gem loaded when the rails project is initialized, so I added the :require => false option.
But in my rails console, after I execute require 'report_service', I cannot find models in this gem.
[1] pry(main)> ReportService
=> ReportService
[2] pry(main)> ReportService::Engine
NameError: uninitialized constant ReportService::Engine
from (pry):2:in `<main>'
[3] pry(main)> require 'report_service'
=> true
[4] pry(main)> ReportService::Engine
=> ReportService::Engine
[5] pry(main)> ReportService::RsExam
NameError: uninitialized constant ReportService::RsExam
from (pry):5:in `<main>'
Here is my report_service gem directory and code:
report_service/lib/report_service.rb
require "active_record/railtie"
require "report_service/engine"
module ReportService
end
report_service/lib/report_service/engine.rb
module ReportService
class Engine < ::Rails::Engine
isolate_namespace ReportService
end
end
report_service/app/models/report_service/rs_exam.rb
module ReportService
class RsExam < ActiveRecord::Base
end
end
Scrap that update. Just add require "report_service/rs_exam" into your report_service.rb.
require "active_record/railtie"
require "report_service/engine"
require "report_service/rs_exam"
module ReportService
end
My reasoning is that what is happening is that your loading the model report_service/rs_exam which is why you will get an uninitialized constant error. Because looking at the console output.
Loading the gem works fine.
require 'report_service'
=> true
The ReportService::Engine is loaded fine.
[4] pry(main)> ReportService::Engine
=> ReportService::Engine
But then when you try to load the rs_exam
[5] pry(main)> ReportService::RsExam
NameError: uninitialized constant ReportService::RsExam
from (pry):5:in `<main>'
you get your uninitialized constant error because it has not been required. Try that and let me know how you get on
Related
I followed all the docs for IronWorkerNG::Client.new and rails:
https://github.com/iron-io/iron_worker_rails_example
My .worker file is doing:
runtime 'ruby'
name 'CompanyList'
merge_gem 'activerecord'
merge_gem 'actionmailer'
merge_gem 'pg'
merge_gem 'aws-s3'
dir '../app/models'
file '../app/mailers/result_mailer.rb'
file "../app/views/result_mailer/email.html.erb", "result_mailer"
exec 'company_list.rb'
remote
and my ruby file does:
require 'action_mailer'
require 'active_record'
require 'aws/s3'
require 'pg'
require 'models/company_list.rb'
require 'result_mailer.rb'
puts "Starting CompanyList worker at #{Time.now}"
ActiveRecord::Base.establish_connection(params['database'])
mailer_config = params['mailer'].inject({}) { |memo, (k, v)| memo[k.to_sym] = v; memo }
#ActionMailer::Base.prepend_view_path('.')
ActionMailer::Base.view_paths = ['.']
ActionMailer::Base.smtp_settings = mailer_config
ActionMailer::Base.delivery_method = :smtp
But everytime I get this error:
/task/__gems__/gems/actionmailer-2.3.18/lib/action_mailer/base.rb:433:
in `method_missing': undefined method `email' for ResultMailer:Class
(NoMethodError)
from /task/models/company_list.rb:16:in `process_file'
from /task/company_list.rb:18:in `<top (required)>'
from /usr/local/lib/site_ruby/1.9.1/rubygems/custom_require.rb:36:in `require'
from /usr/local/lib/site_ruby/1.9.1/rubygems/custom_require.rb:36:in `require'
from __runner__.rb:213:in `<main>'
and ResultMailer DOES have an email method:
class ResultMailer < ActionMailer::Base
def email
to = 'andrew#testdomain.com'
mail :from => 'test#testdomain.com', :to => to, :subject => "Test"
end
end
any idea why action_mailer base line 433 is throwing that error? I can call ResultMailer.email.deliver from the console on my local system just fine. This error is on production with heroku and iron.io.
ah, sometimes writing out the question on stackoverflow makes you see the problem. I needed:
merge_gem "activerecord", "=3.2.8"
merge_gem 'actionmailer', '=3.2.8'
without the 3.2.8 version it was using an old old version of action mailer.
I'm upgrading an app from Rails 3.1 to 3.2 and I'm getting:
NoMethodError (undefined method `write_inheritable_attribute' for #<Class:0x0000010a190dd8>):
app/models/test_file.rb:37:in `<class:TestFile>'
app/models/test_file.rb:1:in `<top (required)>'
app/models/ability.rb:44:in `initialize'
The error applies to this model code:
has_attached_file :logo,
:styles => {:medium => "300x200>", :thumb => "150x60>" },
:storage => :s3,
:s3_credentials => "#{::Rails.root.to_s}/config/s3.yml",
:path => "/logos/:style/:id/:filename"
I have no instances of write_inheritable_attribute anywhere in my app's code. If the above is removed, the error repeats for the next instance of similar code in the app (i.e. everywhere Paperclip is used).
# Gemfile
gem 'rmagick'
gem "paperclip", :git => "git://github.com/thoughtbot/paperclip.git"
I think this issue may be with Paperclip's internal code somewhere. Do you know what's going on here?
:s3_credentials => "#{Rails.root}/config/s3.yml",
My situation is a follows:
I'm running application X
X uses the gem "core"
core extends the models of X with models A, B, C, D
In development this works perfectly. However when I run
(bundle exec) rake RAILS_ENV=staging RAILS_GROUPS=assets assets:precompile
it fails on
** Execute environment
rake aborted!
uninitialized constant A
I tried to fix this issue by placing Rails.application.eager_load! before the Application.initialize! in environments.rb, but I'm afraid that only led to other errors.
Is there a way to include models from an engine in a gem BEFORE the assets:precompile?
I read something about requiring them one by one instead of eager_load all, but the path to the gem differs for every system.
engine.rb in "core":
require 'paperclip'
module Core
class Engine < ::Rails::Engine
config.time_zone = 'Amsterdam'
config.encoding = "utf-8"
config.autoload_paths += %W(#{config.root}/lib/**)
config.generators do |g|
g.test_framework :rspec, :views => false, :fixture => true
g.fixture_replacement :factory_girl, :dir => 'spec/factories'
end
initializer "core.load_app_instance_data" do |app|
Core.setup do |config|
config.app_root = app.root
end
app.class.configure do
#Pull in all the migrations from Commons to the application
config.paths['db/migrate'] += Core::Engine.paths['db/migrate'].existent
end
end
initializer "core.load_static_assets" do |app|
app.middleware.use ::ActionDispatch::Static, "#{root}/public"
end
end
end
I prefer to put any fix in the core gem instead of application X. However if it's not possible X is fine :)
Are you sure the "core" gem is loaded in the staging environment?
I followed the installation procedure of geokit-rails3, here is my conf :
Using rails (3.0.4)
Using activerecord (3.0.4)
Using geokit (1.5.0)
Using geokit-rails (1.1.4)
I get this error "undefined method `within' for #" when i try to query with the "within" method. (note that i am new to rails and maybe missing something obvious)
Here is my class definition :
class Snip < ActiveRecord::Base
belongs_to :user
acts_as_mappable :default_units => :kms,
:default_formula => :sphere,
:distance_field_name => :distance,
:lat_column_name => :latitude,
:lng_column_name => :longitude
end
In my controller i have :
#userLocation = GeoKit::LatLng.new(params[:lat],params[:lng])
#snips = Snip.within(params[:distance], :origin => #userLocation)
Here is what i added to my gemfile :
gem 'geokit', '>= 1.5.0'
gem 'geokit-rails', '1.1.4'
Do you have any idea why i get this error ?
Thanks in advance,
Vincent.
Your gemfile asks for geokit-rails, that's the Rails 2 version. You need to replace that with geokit-rails3
I am trying to use the HTML5 manifest function using a gem called Manifesto. I am stuck on the instructions for usage. I cannot figure out where those settings are supposed to go.
Any ideas? Perhaps a better gem?
https://github.com/johntopley/manifesto#readme
Thankful for all help!
You can put you settings in a file under config/initializers/. Use an informational name (like manifesto.rb). However you don't need a config with the basic usage.
In your Gemfile file, add:
gem 'manifesto'
then install via bundle:
bundle install
create the file app/controllers/manifest_controller.rb
class ManifestController < ApplicationController
def show
headers['Content-Type'] = 'text/cache-manifest'
render :text => Manifesto.cache, :layout => false
end
end
in config/routes.rb add:
match '/manifest' => 'manifest#show'
Restart your app and view the result at http://localhost:3000/manifest
You can pass the option directly to Manifesto.cache like:
# change
render :text => Manifesto.cache, :layout => false
# to
render :text => Manifesto.cache(:directory => './mobile', :compute_hash => false), :layout => false
Or use a YAML file and an initializer.
The config/manifesto.yaml file:
# directory is relative to Rails root
directory: './mobile'
compute_hash: false
The config/initializers/manifesto.rb file:
# Load the config file and convert keys from strings in symbols (the manifesto gem need symbolized options).
MANIFESTO_CONFIG = YAML.load_file(Rails.root.join('config', 'manifesto.yml').to_s).inject({}){|config,(k,v)| config[k.to_sym] = v; config}
And pass the loaded config to Manifesto.cache like:
# change
render :text => Manifesto.cache, :layout => false
# to
render :text => Manifesto.cache(MANIFESTO_CONFIG), :layout => false