Converting Rails 2.3 to Rails 4.0 environment.rb - ruby-on-rails-4

I have an application that runs fine under Rails 2.3.2.
I'm trying to upgrade it to Rails 4.0, using nginx and unicorn.
My config/environment.rb has these lines that don't work:
Rails::Initializer.run do |config|
config.load_paths += %W( #{RAILS_ROOT}/vendor/gems/ #{RAILS_ROOT}/app/exceptions/ )
config.action_controller.session = { :session_key => "my_app", :secret => "c4c6eee57d9af4f2e1a9c56a49d974b7" }
config.gem "mocha", :version => '>=0.9.4'
The error message I get in unicorn.log is
ERROR -- : uninitialized constant Rails (NameError)
/home/art/config/environment.rb:70:in `<top (required)>'
and line 70 is the " Rails::Initializer.run do |config|" line above.
I guess these things have been moved out into some initializer file somewhere?
I guess bundler now handle gem versions, so I should just move the "config.gem... :version ..." lines into my Gemfile?
And where should I set session_key?
Thanks for any advice.

in the new Rails require the application.rb file:
# Load the Rails application.
require File.expand_path('../application', __FILE__)
In the application.rb require rails:
require File.expand_path('../boot', __FILE__)
require 'rails/all'
# Require the gems listed in Gemfile, including any gems
# you've limited to :test, :development, or :production.
Bundler.require(*Rails.groups)
usually the session_key is in a new initializer (under initializers folder - look here)
anyway try to run rake rails:update it might help you change some stuff,
or create new rails project and start to copy things from there.

Related

Rails: NameError: uninitialized constant Article::ImageUploader when using Carrierwave on rails 4.1.5

I'm learning rails 4.1.5
I got this error:
2.1.1 :008 > Article
NameError: uninitialized constant Article::ImageUploader
from /Volumes/disk0s4/www/rails/blog/app/models/article.rb:4:in `<class:Article>'
from /Volumes/disk0s4/www/rails/blog/app/models/article.rb:1:in `<top (required)>'
from (irb):8
from /Users/didin/.rvm/gems/ruby-2.1.1/gems/railties-4.1.5/lib/rails/commands/console.rb:90:in `start'
from /Users/didin/.rvm/gems/ruby-2.1.1/gems/railties-4.1.5/lib/rails/commands/console.rb:9:in `start'
from /Users/didin/.rvm/gems/ruby-2.1.1/gems/railties-4.1.5/lib/rails/commands/commands_tasks.rb:69:in `console'
from /Users/didin/.rvm/gems/ruby-2.1.1/gems/railties-4.1.5/lib/rails/commands/commands_tasks.rb:40:in `run_command!'
from /Users/didin/.rvm/gems/ruby-2.1.1/gems/railties-4.1.5/lib/rails/commands.rb:17:in `<top (required)>'
from /Volumes/disk0s4/www/rails/blog/bin/rails:8:in `<top (required)>'
from /Users/didin/.rvm/rubies/ruby-2.1.1/lib/ruby/2.1.0/rubygems/core_ext/kernel_require.rb:55:in `require'
from /Users/didin/.rvm/rubies/ruby-2.1.1/lib/ruby/2.1.0/rubygems/core_ext/kernel_require.rb:55:in `require'
from -e:1:in `<main>'
my file: article.rb
class Article < ActiveRecord::Base
validates_presence_of :title, :body
belongs_to :user
mount_uploader :image, ImageUploader
end
I got error when running rails console
when I write a word 'Article' on the console, it raises error above, but it working fine when this line mount_uploader :image, ImageUploader at article.rb's file removed.
when that line is restored, the error comes again. so it seems the error is caused that line, but I'm not sure.
anyone can fix this, please...
thank you for reading and answer :-)
I know this is a few months late but I stumbled across this issue myself. My solution was to paste
require 'carrierwave/orm/activerecord'
into the config/environment.rb file. Just append it at the end.
My Env:
Ruby 2.1.2p95 ;
Rails 4.1.7 ;
Carrierwave-0.10.0
I added this to application.rb
require 'carrierwave'
require 'carrierwave/orm/activerecord'
Rails 4.2.0, Ruby 2.2.2, Carrierwave 0.10.0
Ill put it here, just in case..
IF you are using spring gem then you have to "restart" it by changing config/application.rb or close and open terminal, or: $ bin/spring stop
You can inspect its processes lifetime here(scroll to right):
$ ps aux | grep spring
alexey 55936 0.0 0.9 2645908 78440 ?? Ss Thu06PM 0:13.17 spring app | myapp | started 26 hours ago | development mode
alexey 81963 0.0 0.0 2481764 1608 ?? S Sat11PM 0:01.91 spring server | myapp | started 141 hours ago
And kill it if needed.
More info at: https://github.com/rails/spring
Are you using spring?
I got two terminal windows, one for server, the other for console.
After I restarted my server and enter console again, the error disappeared.
And I didn't add any line in application.rb
Rails 4.2.4, Ruby 2.3.0, Carrierwave 0.10.0 a2c93fe
I'm assuming you're using CarrierWave gem for file uploads. Have you checked that it was properly installed?
You could issue the command to check:
bundle show carrierwave
In my case, I've bundle installed on a different terminal where I ran rails console. HTH!
Another tip try: Open up the uploaded that was just generated. Ensure the name of the uploader class matches the name of the class you added in your model.
If error appears in rails app' specs only you may be missing
require 'rails_helper'
at the top :)
Exit and start your console again.
In this case just reload! won't solve.
In my case I forgot to run rails g uploader image. Once done, it works fine.
I was getting this error and then it worked after restarting the server.
This occurs rather frequently. In development, the error crops up. The uploader was probably just added. Removing spring resolves, but that is a band-aid. The simple server re-start is often the proper solution.
try this
in appliccation.rb correct module name
this fix same problem for me
module CorrectName #OldName <---------------
# Do not swallow errors in after_commit/after_rollback callbacks.
config.active_record.raise_in_transactional_callbacks = true
end
end
For me, I uninstalled both the below gems
gem 'spring'
gem 'spring-watcher-listen', '~> 2.0.0'
In my case in my 'user.rb' module file, I had added the following lines:
mount_uploader :photo, PhotoUploader
mount_uploader :coverimage, CoverimageUploader
I had to quit them and
For anyone who has a preloader, such as Spring in place, before uninstalling the otherwise useful preloaded that speeds up your app, it's worth stopping it so that load paths will be reinitialised and any uploader in app/uploaders will be loaded once a new console or a rails process start. Try bin/spring stop. It will restart once it's necessary, now with the uploaders in place.

Disabling the running-twice with Zeus in Rails 4 minitest?

Zeus has a known issue where it runs specs double if you include the default "autorunner" for a testing suite.
It is common to see tests running twice when starting out with Zeus.
If you see your tests/specs running twice, you should try disabling
require 'rspec/autotest' and require 'rspec/autorun' (for RSpec), or
require 'minitest/autorun' (for Minitest). (see #134 for more
information).
https://github.com/burke/zeus#important
However, Rails4 minitest includes "rails/test_help" which is a file in Railties 4.0.0; which includes yet another custom runner. And does other generic of setup and configuration.
For Rails4 with default minitest, there is no simple include to leave out, it seems.
Is there a solution for this?
i was just facing this same problem with Rails4+minitest+zeus.
After i include gem "minitest-rails-shoulda" in my Gemfile, zeus starts to run tests twice.
I just changed the gem orders at Gemfile and it solves!
Gemfile with Zeus running twice:
group :test do
gem "minitest-rails-shoulda"
gem 'minitest'
#...
end
Gemfile CORRECT:
group :test do
gem 'minitest'
gem "minitest-rails-shoulda"
#....
end

can't install simple_captcha produces uninitialized constant Sprockets::Helpers

I have included the simple_captcha gem as instructed on their site:
gem 'simple_captcha', :git => 'git://github.com/galetahub/simple-captcha.git'
I have then run bundler to install it.
Finally when I go to run the following command I get an error:
rails generate simple_captcha
.rvm/gems/ruby-1.9.3-p327#eapi4/bundler/gems/simple-captcha-e99cc7e8bf6b/lib/simple_captcha/form_builder.rb:7:in `included': uninitialized constant Sprockets::Helpers (NameError)
I've searched on the web and can't find any other users with this problem.
Any help appreciated
They say in this pull request that the plugin doesn't support rails 4, You can download their branch and try it out.
Looking at the diff it shows your problem
- base.send(:include, Sprockets::Helpers::RailsHelper)
- base.send(:include, Sprockets::Helpers::IsolatedHelper)
+ base.send(:include, Sprockets::Rails::Helper)
https://github.com/galetahub/simple-captcha/pull/39/files
you could this as the previous version of simple_captcha is not supported on rails 3.
Azdaroth forked of simple_captcha
gem 'simple_captcha', :git => 'git://github.com/Azdaroth/simple-captcha.git', :branch => 'rails-4'

Puppet install gem fails but gem is installed anyways

I have a Puppet manifest that installs the Zurb Foundation gem:
class compass {
package { ["rubygems"]:
ensure => 'installed'
}
package { ['zurb-foundation']:
ensure => '3.0.6',
provider => 'gem',
require => Package['rubygems']
}
}
This causes the following error when running puppet apply:
err: /Stage[main]/Compass/Package[zurb-foundation]/ensure: change from absent to 3.0.6 failed: Could not update: Execution of '/usr/bin/gem install -v 3.0.6 --include-dependencies --no-rdoc --no-ri zurb-foundation' returned 1: INFO: `gem install -y` is now default and will be removed
INFO: use --ignore-dependencies to install only the gems you list
Building native extensions. This could take a while...
ERROR: Error installing zurb-foundation:
ERROR: Failed to build gem native extension.
/usr/bin/ruby1.9.1 extconf.rb
/usr/lib/ruby/1.9.1/rubygems/custom_require.rb:36:in `require': cannot load such file -- mkmf (LoadError)
from /usr/lib/ruby/1.9.1/rubygems/custom_require.rb:36:in `require'
from extconf.rb:4:in `<main>'
Gem files will remain installed in /var/lib/gems/1.9.1/gems/ffi-1.9.0 for inspection.
Results logged to /var/lib/gems/1.9.1/gems/ffi-1.9.0/ext/ffi_c/gem_make.out
at /etc/puppet/manifests/classes/compass.pp:25
However, doing a gem list yields the following results:
# gem list
*** LOCAL GEMS ***
chunky_png (1.2.8)
compass (0.12.2)
fssm (0.2.10)
rake (10.1.0)
rb-fsevent (0.9.3)
sass (3.2.0.alpha.277)
zurb-foundation (3.0.6)
The error causes puppet apply to not complete properly, since a lot of items are skipped because of failed dependencies. What can I do?
I think that the problem is that you don't have the ruby dev kit installed for you ruby (that's what's used to build native extensions).
You'll want to add something like (it may be different depending on your version of ruby):
package { 'ruby-dev':
ensure => installed,
before => Package['zurb-foundation'],
}
This should allow the gem to be built properly and let your puppet run finish too.

Handlebars Asset not Compiling with ember-rails after Upgrade to Rails 4

I have a Rails app with Ember on the front-end. Today, I upgraded to Rails 4 and the only issue I cannot resolve is that my Handlebars templates are not being compiled or included as an asset (in development mode). I'm not getting any error messages.
I generated a brand new Rails 4 test app and installed Ember and it serves the Handlebars files just fine. All gems in the test app are present in my app. I tried running rake rails:upgrade, and that had no effect. I tried modifying the ember-rails source to output log messages and the messages and execution paths were the same for the test app and my app.
I'm requiring the templates the same way I always did:
require_tree ./templates
I tried using ember-rails 0.11.1 and from Github master, with no luck.
Related gem versions:
* ember-data-source (0.0.5)
* ember-rails (0.11.1 4dc902b)
* ember-source (1.0.0.rc2.0)
* sprockets (2.9.0)
* sprockets-rails (2.0.0.rc3)
* barber (0.4.1)
Templates are all in app/assets/javascripts/templates and all use the extension .handlebars. Although I tried .hbs and .js.hjs too.
From the Chrome console:
Ember.TEMPLATES
Object {}
I am able to manually compile assets from the Rails command line like this:
environment = Sprockets::Environment.new
environment.append_path 'app/assets/javascripts'
a = environment['templates/test.handlebars']
a.to_s => "..compiled template.."
And the engine is registered:
environment.engines
=> {..".handlebars"=>Ember::Handlebars::Template..}
Rails.application.config.assets.paths includes the app/assets/javascripts folder, as it should.
Any help with this would be greatly appreciated, Thanks!
I've narrowed it down to the following block in ember-rail's engine.rb file. For my app, it is sending register_engine on Sprockets, not app.assets. On the fresh Rails 4 test app, it was also doing this. However, in my app when I force the execution to use app.assets, the handlebar templates are compiled.
I believe since my app was upgraded from Rails 3, somewhere some configuration is missing to properly use Sprockets. I'll post more when I know.
initializer "ember_rails.setup", :after => :append_assets_path, :group => :all do |app|
sprockets = if ::Rails::VERSION::MAJOR == 4
Sprockets.respond_to?('register_engine') ? Sprockets : app.assets
else
app.assets
end
sprockets.register_engine '.handlebars', Ember::Handlebars::Template
sprockets.register_engine '.hbs', Ember::Handlebars::Template
sprockets.register_engine '.hjs', Ember::Handlebars::Template
end
UPDATE:
For me,changing the railties order in application.rb was enough to fix the problem. I believe some other engine was clearing the Sprockets config, so by loading the Ember::Rails engine later in the process, I was able to hack a way around that issue.
config.railties_order = [:main_app, :all, Ember::Rails::Engine]