I'm getting an odd error, where no rails commands of any kind are executing. Whenever I enter a rails command I get the following:
Ignoring racc-1.5.2 because its extensions are not built. Try: gem pristine racc --version 1.5.2
(this is repeated about 100 times before the following):
bin/rails:2:in `load': /Users/robertmorris/Desktop/All Projects/Code School/my9s/bin/spring:11: syntax error, unexpected keyword_rescue, expecting keyword_end (SyntaxError) rescue Gem::LoadError ^ /Users/robertmorris/Desktop/All Projects/Code School/my9s/bin/spring:14: syntax error, unexpected keyword_end, expecting end-of-input from bin/rails:2:in `<main>'
My bin/spring file:
if !defined?(Spring) && [nil, "development", "test"].include?(ENV["RAILS_ENV"])
gem "bundler"
require "bundler"
# Load Spring without loading other gems in the Gemfile, for speed.
Bundler.locked_gems&.specs&.find { |spec| spec.name == "spring" }&.tap do |spring|
Gem.use_paths Gem.dir, Bundler.bundle_path.to_s, *Gem.path
gem "spring", spring.version
require "spring/binstub"
rescue Gem::LoadError
# Ignore when Spring is not installed.
end
end
Thanks!
You are using an old (unsupported) version of ruby.
You should upgrade it using any available local version manager like rvm, rbenv or asdf, to at least 2.5, or better 2.7 or 3.
On ruby 2.4 and previous, you could only rescue inside either a method:
def foo
# do something
rescue
# do something else
end
or an explicit begin/end block:
begin
# do something
rescue
# do something else
end
Since ruby 2.5 was released, all do/end blocks can have rescue clauses:
foo.bar.baz.tap do
# do something
rescue
# do something else
end
while before that you had to
foo.bar.baz.tap do
begin
# do something
rescue
# do something else
end
end
Related
It's possible, for example, to use ActiveRecord inline in a Ruby script. I like this a lot to report bugs, test features and share gists.
I'm wondering if the same could be done for a Rails webservice? (I'm mainly interested in getting the controller layer to work, the rest should be easy to add on demand.) Something along these lines:
begin
require "bundler/inline"
rescue LoadError => e
$stderr.puts "Bundler version 1.10 or later is required. Please update your Bundler"
raise e
end
gemfile(true) do
source "https://rubygems.org"
gem 'rails', '~> 6.0.0'
end
require 'rails/commands'
APP_PATH = File.expand_path('config/application', __dir__)
Rails::Command.invoke('server')
When toying around with this it did seem that an external entry point (APP_PATH) is required. So alternatively, an acceptable approach would be to cram all config into the single entry point. I couldn't get this to work so far.
The Rails Initialization Process is the best resource I found about this so far.
I produced a minimal Rails app to get started as follows:
rails new min-rails --skip-keeps --skip-action-mailer --skip-action-mailbox --skip-action-text --skip-active-record --skip-active-storage --skip-puma --skip-action-cable --skip-sprockets --skip-spring --skip-listen --skip-javascript --skip-turbolinks --skip-test --skip-system-test --skip-bootsnap --api
cd min-rails
rm -rf app/jobs app/models config/initializers config/locales lib log public tmp vendor config/environments/test.rb config/environments/production.rb config/credentials.yml.enc config/master.key bin/rake bin/setup bin/bundle
I ended up with the following script:
inline-rails.rb
begin
require "bundler/inline"
rescue LoadError => e
$stderr.puts "Bundler version 1.10 or later is required. Please update your Bundler"
raise e
end
gemfile(true) do
source "https://rubygems.org"
gem 'rails', '~> 6.0.0'
end
require "action_controller/railtie"
class App < Rails::Application
routes.append do
get "/hello/world" => "hello#world"
end
config.consider_all_requests_local = true # display errors
end
class HelloController < ActionController::API
def world
render json: {hello: :world}
end
end
App.initialize!
Rack::Server.new(app: App, Port: 3000).start
Run it as:
ruby inline-rails.rb
I have the following ruby code :
require 'HTTPClient'
require 'rubygems'
require 'zip'
def self.unzip(data, dest_dir)
::Zip::File.open_buffer(data) do |fzip|
fzip.each do |entry|
path = File.join(dest_dir, entry.name)
puts "here"
FileUtils::mkdir_p(File.dirname(path))
fzip.extract(entry, path) unless File.exist?(path)
#fzip.close
end
end
end
def self.fetch_from_url(url, dest_dir)
response = HTTPClient.get(url, follows_redirect: true)
if response.status == 200
unzip(response.body, dest_dir)
else
raise 'Could not fetch files from 3scale'
end
end
url = 'link/artifactory/zip-release-local/djin/3Sroxy/1.0.5/configuration.zip'
fetch_from_url(url, "/Users/something/")
When I run this in Mac ruby 2.0.0p481 it works fine and unzips the folder, but when I run the same in centOS 6.6 in ruby 1.8.7 it fails with following :
[root#ip-10-201-90-206 ~]# sudo ruby test/ex.rb
test/ex.rb:7:in `unzip': uninitialized constant Zip::File (NameError)
from test/ex.rb:20:in `fetch_from_url'
from test/ex.rb:28
also, I've done :
gem install zip
Successfully installed zip-2.0.2
1 gem installed
Installing ri documentation for zip-2.0.2...
Installing RDoc documentation for zip-2.0.2...
As Prashant4224 stated, you need to install
gem install rubyzip
You seem to use the zip Gem...
In Gemfile you need to add. Everything else didn't work for me.
gem 'rubyzip', '>= 1.0.0'
gem 'zip-zip'
/home/palpandi/.rvm/gems/ruby-1.8.7-p374#fedena_zip/gems/rails-2.3.5/lib/rails/gem_dependency.rb:119:Warning:
Gem::Dependency#version_requirements is deprecated and will be removed
on or after August 2010. Use #requirement
/home/palpandi/.rvm/gems/ruby-1.8.7-p374#fedena_zip/gems/activesupport-2.3.5/lib/active_support/dependencies.rb:105:in
`const_missing': uninitialized constant Rails::Boot::Bundler
(NameError)
Using Rails 2.3.5
Ruby 1.8.7
ubuntu 12.04
I had a similar problem. The correct way to solve this is to go to your project folder, then in config/boot.rb go to the very bottom, and just before the Rails.boot! line add the following :
begin
require "rubygems"
require "bundler"
rescue Bundler::GemNotFound
raise RuntimeError, "Bundler couldn't find some gems." + "Did you run bundle install?"
end
class Rails::Boot
def run
load_initializer
Rails::Initializer.class_eval do
def load_gems
#bundler_loaded ||= Bundler.require :default, Rails.env
end
end
Rails::Initializer.run(:set_load_path)
end
end
This will solve the "uninitialized constant Authorization" error.
put this line in your boot.rb
begin
require "rubygems"
require "bundler"
rescue Bundler::GemNotFound
raise RuntimeError, "Bundler couldn't find some gems." + "Did you run bundle install?"
end
or
gem install bundler
and after adding:
gem 'bundler'
in line 2 in config/boot.rb (just after require 'rubygems')
There is a module
require 'iconv'
module Escape
def escape(string)
return_value = Iconv.conv('ascii//translit//IGNORE', 'utf-8', string).to_s
end
end
It`s work in 1.8.7 but not in 1.9.1
The error message is "NameError (uninitialized constant Escape::Iconv)"
and the follow is work in 1.9.1,Why??????? (my rails is rails 3 in ubuntu)
module Escape
def escape(string)
require 'iconv'
return_value = Iconv.conv('ascii//translit//IGNORE', 'utf-8', string).to_s
end
end
don't use 1.9.1 for rails3, instead use 1.9.2 or 1.8.7 . read here in the comments: http://weblog.rubyonrails.org/2010/2/5/rails-3-0-beta-release
Ok, so I have a Rails app set up on DreamHost and I had it working a while ago and now it's broken. I don't know a lot about deployment environments or anything like that so please forgive my ignorance. Anyway, it looks like the app is crashing at this line in config/environment.rb:
require File.join(File.dirname(__FILE__), 'boot')
config/boot.rb is pretty much normal, but I'll include it here anyway.
# Don't change this file!
# Configure your app in config/environment.rb and config/environments/*.rb
RAILS_ROOT = "#{File.dirname(__FILE__)}/.." unless defined?(RAILS_ROOT)
module Rails
class << self
def boot!
unless booted?
preinitialize
pick_boot.run
end
end
def booted?
defined? Rails::Initializer
end
def pick_boot
(vendor_rails? ? VendorBoot : GemBoot).new
end
def vendor_rails?
File.exist?("#{RAILS_ROOT}/vendor/rails")
end
def preinitialize
load(preinitializer_path) if File.exist?(preinitializer_path)
end
def preinitializer_path
"#{RAILS_ROOT}/config/preinitializer.rb"
end
end
class Boot
def run
load_initializer
Rails::Initializer.run(:set_load_path)
end
end
class VendorBoot < Boot
def load_initializer
require "#{RAILS_ROOT}/vendor/rails/railties/lib/initializer"
Rails::Initializer.run(:install_gem_spec_stubs)
end
end
class GemBoot < Boot
def load_initializer
self.class.load_rubygems
load_rails_gem
require 'initializer'
end
def load_rails_gem
if version = self.class.gem_version
gem 'rails', version
else
gem 'rails'
end
rescue Gem::LoadError => load_error
$stderr.puts %(Missing the Rails #{version} gem. Please `gem install -v=#{version} rails`, update your RAILS_GEM_VERSION setting in config/environment.rb for the Rails version you do have installed, or comment out RAILS_GEM_VERSION to use the latest version installed.)
exit 1
end
class << self
def rubygems_version
Gem::RubyGemsVersion if defined? Gem::RubyGemsVersion
end
def gem_version
if defined? RAILS_GEM_VERSION
RAILS_GEM_VERSION
elsif ENV.include?('RAILS_GEM_VERSION')
ENV['RAILS_GEM_VERSION']
else
parse_gem_version(read_environment_rb)
end
end
def load_rubygems
require 'rubygems'
min_version = '1.1.1'
unless rubygems_version >= min_version
$stderr.puts %Q(Rails requires RubyGems >= #{min_version} (you have #{rubygems_version}). Please `gem update --system` and try again.)
exit 1
end
rescue LoadError
$stderr.puts %Q(Rails requires RubyGems >= #{min_version}. Please install RubyGems and try again: http://rubygems.rubyforge.org)
exit 1
end
def parse_gem_version(text)
$1 if text =~ /^[^#]*RAILS_GEM_VERSION\s*=\s*["']([!~<>=]*\s*[\d.]+)["']/
end
private
def read_environment_rb
File.read("#{RAILS_ROOT}/config/environment.rb")
end
end
end
end
# All that for this:
Rails.boot!
Does anyone have any ideas? I am not getting any errors in the log or on the page.
-fREW
I had the same problem on DreamHost. Freezing rails and unpacking all gems got me past it.
rake rails:freeze:gems
rake gems:unpack:dependencies
My guess would be that you're breaking because of a newer version of the Rails gems on Dreamhost. At least, that's been my issue when things have blown up in something like boot.rb.
Try freezing the gems from your development environment into your vendor/rails directory.
Ya - the problem is not really in boot.rb - it's just that boot.rb is where rails is actually loaded.
So you'll get an error like this if you've specified a version of Rails that just doesn't exist on your dreamhost slice. This can happen if you either upgrade your project, start a new project (and forget that you upgraded rails in the meanwhile) or if you're still using an old version of rails and it's now been removed from the dreamhost server that you're on.
To figure out which is is, look in config/environment.rb for the line that'll read something like:
RAILS_GEM_VERSION = '2.3.4' unless defined? RAILS_GEM_VERSION
Then ssh to your dreamhost server and type gem list and see if your version is in the list.
If not, you an try several options. Lets say the version you're using is 2.3.4
To begin with, try: gem install rails -v=2.3.4 then restart. That may be all that is required.
If that doesn't work, then try freezing and unpacking the gems (as per the other answer here).
There is also another possibility - that you're actually missing a gem that rails depends on but which is failing silently - eg a dependency on a certain version of rack caught me out once. But you may also have other gem dependencies
If you run rake gems you will be able to list all the gems that your project knows about that it needs - make sure they're installed to begin with.
Then, as a sort of rough smoke-test, try running script/console - if you're missing an important rails gem, script/console won't load and should fail, giving you a notice about the gem you need.
Update:
If you're trying to run v 2.3.5, you may also be suffering from this problem:
Bypassing rack version error using Rails 2.3.5
In which case you'll need to follow the instructions there.