uninitialized constant Moped::BSON in rails 4 app on heroku - heroku

I just updated from rails 3 to rails 4. Everything works locally, but deployed on heroku, I get the following error:
ActionView::Template::Error (uninitialized constant Moped::BSON):
3: %nav.navbar-collapse
4: %ul.nav
5: %li
6: - if user_signed_in?
7: = link_to 'Logout', destroy_user_session_path, :method=>'delete'
8: - else
9: = link_to 'Login', new_user_session_path
app/views/layouts/_navigation.html.haml:6:in `_app_views_layouts__navigation_html_haml___1118031947301940708_70104067139880'
app/views/layouts/application.html.haml:18:in `_app_views_layouts_application_html_haml__1093647294459268715_70104069850820'
The same error occurs in other haml files when I access current_user
- if current_user
...

The following worked for me:
add to Gemfile:
gem "bson"
gem "moped", github: "mongoid/moped"
bundle install
add to application.rb:
require "bson"
require "moped"
Moped::BSON = BSON
Answer from:
https://github.com/mongoid/mongoid/issues/3455

See this comment from Moped's author as of Moped 2.0.0 (which is the version , as of this writing, used as the driver in mongoid 4.0.0):
Moped's BSON implementation has been removed in favor of the 10gen bson gem 2.0 and higher. All Moped::BSON references should be changed to just BSON.
https://github.com/mongoid/moped/blob/master/CHANGELOG.md

This error can also be caused by having references to Moped::BSON in serialized cookies/session. Deleting cookies fixes it then.

Related

undefined method `escape' for URI:Module in ruby 3

After updating my Rails application from Rails 6.0.1 to Rails 7.0.2.3
I am getting issue with the gem "paperclip", '~> 6.1.0'
while using it in application is gives error:
ActionView::Template::Error (undefined method `escape' for URI:Module
Did you mean? escape_once):
Usage in my application:
<%= image_tag current_user.image.url('med'), width: "36px" %>
How to resolve this issue when bug is present in the ruby gemfile itself, thanks in advance.
The solution to this situation do a monkey patching to the missing method in library.
add a ruby filke uri_escape.rb inside the initializers folder:
add lines for monkey patching:
module URI
def URI.escape(url)
url
end
end
and its done.

Why does rspec puppet fail with undefined method error when switching ruby versions?

I developed my puppet module with ruby version 1.8.7 ran bundle install on the following gem file and rspec tests worked successfully (so did the puppet run):
source 'https://rubygems.org'
if puppetversion = ENV['PUPPET_GEM_VERSION']
gem 'puppet', puppetversion, :require => false
else
gem 'puppet', '3.7.5'
end
gem 'metadata-json-lint'
gem 'puppetlabs_spec_helper', '>= 0.1.0'
gem 'puppet-lint', '>= 1.0.0'
gem 'facter', '>= 1.7.0'
gem 'rspec-puppet-facts'
# rspec must be v2 for ruby 1.8.7
if RUBY_VERSION >= '1.8.7' and RUBY_VERSION < '1.9'
gem 'rspec', '~> 2.0'
end
However when I did a bundle install on the same Gemfile on my CI server which has ruby 1.9.3 I encountered the following rspec failures:
See verbose errors here
The common erorr is:
Puppet::Error:
Failed to parse template silex/var/config/settings.json.erb:
Filepath: /var/tmp/silex/spec/fixtures/modules/silex/templates/var/config/settings.json.erb
Line: 4
Detail: undefined method `each_with_index' for "default":String
Template that uses the each_with_index method:
{
"elasticsearch.settings": {
"connections": [
<% #elastic_hosts.each_with_index do |host,i| -%>
{"host":"<%= host['ip'] %>", "port": <%= host['port'] %> }<%= ',' if i < (#elastic_hosts.size - 1) -%>
<% end -%>
],
"index": "<%= #elastic_index %>"
}
},
"cache.lifetime": <%= #elastic_cache_lifetime %>,
"environment": "dev",
"trusted": [
<% #trusted_hosts.each_with_index do |host,i| -%>
{"host":"<%= host['host'] %>"}<%= ',' if i < (#trusted_hosts.size - 1) -%>
<% end -%>
],
"homepage.settings": {
"hero.count": 20,
"list.count": 20
}
}
I'm failing to understand why this would fail with a different version of ruby? I've also tried ruby 2.1.1 with the same result. The results are only successful on the CI server with ruby 1.8.7 (same version I used for development of the module).
UPDATE 1
It appears that because elastic_hosts is a hash map and I had a string it was failing. I did fix this along with trusted_hosts (another hash map) which has resulted in most of the tests passing but the error persists.
See errors here
NOTE: The error is unchanged from the common error highlighted above.
I have also updated the template file above with the full contents.
My config spec file
If you look at my config spec file I am not specifying default as a string anywhere yet it tells me that it is the error..
FIX
As suspected my init spec class had defaults set and adding the hiera look ups for elastic_hosts and trusted_hosts fixed the issue.
UPDATE 2
This behaviour is reproducible between version 1.8.7 and version > 1.9.3. It seems to have something to with how hashes are treated.
The answer is pretty simple: Because the the each method was removed from newer versions of Ruby. One reason was that the method was confusing in the context of UTF-8 strings: does each iterate over each char or each byte?
But there is each_char and you can combine that with with_index:
string.each_char.with_index do |char, i|
# ...
end
Furthermore you call that method on #elastic_hosts what in the snippet clearly should be an array of hosts. But the error message tells us that the #elastic_hosts is the string default. So it makes sense that the method call with each_with_index fails, but it seems like the iteration itself does not make sense. I advise to check if #elastic_hosts is set correctly.

Why does Rails demo app give "uninitialized constant ExecJS::RubyRacerRuntime::Context::V8"?

Being new to Rails, I'm trying to get a basic "hello world" app up and running. Specifically the app in chapter 2 of Agile Web Development with Rails. Loading up the hello world page results in the error given in the title.
So far I've done the following:
rails new demo
cd demo
Added gem 'therubyracer' to the Gemfile after ExecJS complained about no JavaScript runtime being available.
bundle install
rake about # (see below)
rails generate controller Say hello goodbye
rails server
At this point WEBrick fires up and I load up http://localhost:3000/say/hello. WEBrick logs the following error (which also appears in the browser):
Started GET "/say/hello" for 127.0.0.1 at 2012-10-26 11:45:10 -0400
Connecting to database specified by database.yml
Processing by SayController#hello as HTML
Rendered say/hello.html.erb within layouts/application (80.9ms)
Completed 500 Internal Server Error in 167ms
ActionView::Template::Error (uninitialized constant ExecJS::RubyRacerRuntime::Context::V8
(in /home/error/work/demo/app/assets/javascripts/say.js.coffee)):
3: <head>
4: <title>Demo</title>
5: <%= stylesheet_link_tag "application", :media => "all" %>
6: <%= javascript_include_tag "application" %>
7: <%= csrf_meta_tags %>
8: </head>
9: <body>
app/views/layouts/application.html.erb:6:in `_app_views_layouts_application_html_erb___1746644185114933585_70069037653480'
Rendered /home/error/.gem/ruby/1.9.1/gems/actionpack-3.2.8/lib/action_dispatch/middleware/templates/rescues/_trace.erb (3.7ms)
Rendered /home/error/.gem/ruby/1.9.1/gems/actionpack-3.2.8/lib/action_dispatch/middleware/templates/rescues/_request_and_response.erb (2.2ms)
Rendered /home/error/.gem/ruby/1.9.1/gems/actionpack-3.2.8/lib/action_dispatch/middleware/templates/rescues/template_error.erb within rescues/layout (18.7ms)
[2012-10-26 11:45:12] WARN Could not determine content-length of response body. Set content-length of the response or set Response#chunked = true
rake about returns:
About your application's environment
Ruby version 1.9.3 (x86_64-linux)
RubyGems version 1.8.24
Rack version 1.4
Rails version 3.2.8
JavaScript Runtime therubyracer (V8)
Active Record version 3.2.8
Action Pack version 3.2.8
Active Resource version 3.2.8
Action Mailer version 3.2.8
Active Support version 3.2.8
Middleware ActionDispatch::Static, Rack::Lock, #<ActiveSupport::Cache::Strategy::LocalCache::Middleware:0x0000000276b298>, Rack::Runtime, Rack::MethodOverride, ActionDispatch::RequestId, Rails::Rack::Logger, ActionDispatch::ShowExceptions, ActionDispatch::DebugExceptions, ActionDispatch::RemoteIp, ActionDispatch::Reloader, ActionDispatch::Callbacks, ActiveRecord::ConnectionAdapters::ConnectionManagement, ActiveRecord::QueryCache, ActionDispatch::Cookies, ActionDispatch::Session::CookieStore, ActionDispatch::Flash, ActionDispatch::ParamsParser, ActionDispatch::Head, Rack::ConditionalGet, Rack::ETag, ActionDispatch::BestStandardsSupport
Application root /home/error/work/demo
Environment development
Database adapter sqlite3
Database schema version 0
The workstation is running Fedora 17 x86_64. Despite that, I've pretty much ignored the ruby gems in the yum repositories (I think) and used gem install in my home directory to get everything I need. (Again, I think.)
Being new to Ruby and Rails, I'm pretty sure I've missed some obvious convention that I'm not yet aware of. I'm also not quite sure what additional information I might need to provide, so please let me know that as well.
Please try this:
sudo apt-get install nodejs
rubytheracer and nodejs are both javascript libraries. Most folks are going for nodejs these days.

Delayed_job: Job failed to load: uninitialized constant Syck::Syck

Here's the error:
>> Delayed::Job.find(:last).last_error
=> {Job failed to load: uninitialized constant Syck::Syck. Handler: \"--- !ruby/struct:Delayed::PerformableMethod \\nobject: &id007 !ruby/object:TryController \\n _action_name: create
but I have syck ext installed.
Usage:
def create_user(name,pass,time)
puts "hello"
Net::HTTP.get(URI.parse("http://www.example.net/buildtest.php?hao=#{name}&mi=#{pass}&da=#{time}"))
end
def create
delay.create_user("nihao000oei9","1","1")
end
gem 'delayed_job', '2.1.4'
ruby 1.9.2p0 (2010-08-18 revision 29036) [x86_64-darwin10.4.0]
Rails 3.0.1
thanks.
I found this as an issue when I upgraded from Rails 2 to 3 and deployed to the Heroku Cedar stack. It's an issue when Delayed_Job attempts to deserialise the handler from the Job queue item. By default it uses the Syck YAML parser.
Turns out Syck isn't available on the Heroku Cedar stack.
Including the Psych gem and redeploying fixed it for me and allow me to re-invoke the failed jobs.
gem 'psych'
I just ran into this and it turned out to be a syntax error in my just-modified database.yml.
It was using a bad reference for cucumber:
test: &test
database: test
cucumber:
<<: &base

Rails 3 Authlogic - 'acts_as_authentic' undefined

I'm getting the following error:
NameError (undefined local variable or method `acts_as_authentic' for #<Class:0x1037e6310>):
app/models/user.rb:2
app/controllers/user_controller.rb:3:in `new'
I'm using Rails 3.0.0, with Ruby 1.8.7. Authlogic is in my Gemfile as follows:
gem 'authlogic', :git => "git://github.com/binarylogic/authlogic.git"
The entire contents of my User.rb file are as follows:
class User < ActiveRecord::Base
acts_as_authentic
end
I get the same error whether through 'rails console' or through the server. I have restarted the server more times than I can count. Please help, I can't figure it out.
Use a version of authlogic patched for Rails 3
gem 'authlogic', :git => 'git://github.com/odorcicd/authlogic.git', :branch => 'rails3'
Or even better. Use Devise
Is the Authlogic Gem installed ?.
Please do
Bundle install
this should solve your problem.
gem "authlogic", "2.1.6"
Then
Bundle install
Hope it'll helpfull for you. :)
create a file in config/initializers as restful_authentication.rb
and paste this inside the file and restart server and try
require 'authenticated_system'
require 'restful_authentication/authentication'
require 'restful_authentication/authentication/by_password'
require 'restful_authentication/authentication/by_cookie_token'
require 'restful_authentication/authorization/aasm_roles'
require 'restful_authentication/authorization/stateful_roles'
require 'restful_authentication/trustification/email_validation'

Resources