Can not run rails server for my application - ruby-on-rails

I am trying to run a project downloaded from internet, it is ruby on rails, I am getting the following error
Traceback (most recent call last):
4: from bin/rails:3:in `<main>'
3: from bin/rails:3:in `load'
2: from C:/Users/Amira/canvas/bin/spring:10:in `<top (required)>'
1: from C:/Users/Amira/canvas/bin/spring:10:in `read'
C:/Users/Amira/canvas/bin/spring:10:in `read': No such file or directory # rb_sysopen - C:/Users/Amira/canvas/Gemfile.lock (Errno::ENOENT)
The code written in the file is as following
#!/usr/bin/env ruby
# This file loads spring without using Bundler, in order to be fast.
# It gets overwritten when you run the `spring binstub` command.
unless defined?(Spring)
require 'rubygems'
require 'bundler'
if (match = Bundler.default_lockfile.read.match(/^GEM$.*?^ (?: )*spring \((.*?)\)$.*?^$/m))
Gem.paths = { 'GEM_PATH' => [Bundler.bundle_path.to_s, *Gem.path].uniq.join(Gem.path_separator) }
gem 'spring', match[1]
require 'spring/binstub'
end
end
It is an opensource project and I am new on rails
can anyone please help me ?
EDIT
Gemfile content
# What have they done to the Gemfile???
#
# Relax. Breathe deep. All the gems are still there; they're just loaded in various files in Gemfile.d/
# This allows us to require gems locally that we might not want to commit to our public repo. We can maintain
# a customized list of gems for development and debuggery, without affecting our ability to merge with canvas-lms
#
# NOTE: this file has to use 1.8.7 hash syntax to not raise a parser exception on 1.8.7
#
# NOTE: some files in Gemfile.d/ will have certain required gems indented. While this may seem arbitrary,
# it actually has semantic significance. An indented gem required in Gemfile is a gem that is NOT
# directly used by Canvas, but required by a gem that is used by Canvas. We lock into specific versions of
# these gems to prevent regression, and the indentation serves to alert us to the relationship between the gem and canvas-lms
source 'https://rubygems.org/'
Dir[File.join(File.dirname(__FILE__), 'gems/plugins/*/Gemfile.d/_before.rb')].each do |file|
eval(File.read(file), nil, file)
end
require File.expand_path("../config/canvas_rails_switcher", __FILE__)
Dir.glob(File.join(File.dirname(__FILE__), 'Gemfile.d', '*.rb')).sort.each do |file|
eval(File.read(file), nil, file)
end

The Gemfile usually specifies the dependencies (other code the application needs). The project you look into seems to use a slightly more complex setup.
Normally it works roughly like this:
Once the dependencies are installed via bundler (the command bundle or the more explicit bundle install), a Gemfile.lock is placed next to it to list the actually used versions of the installed dependencies.
Two takeaways:
use bundle (the gem is called bundler) to install dependencies.
read the README of the project carefully. If it does not have instructions on installation, create an issue if the projects has a method to do so. If you are hypernice or normal and the project is open and healthy, create a Pull Request for the README in which you describe how you managed to install the app.
Edit this might have sounded a bit arrogant, and I did not peek into the details. I assume you followed https://github.com/instructure/canvas-lms/wiki/Quick-Start . The canvas-lms project uses a setup slightly more complex than your typical Rails application. I believe you should ask specifically for help with canvas installation (edit your question).

Related

Can't mount newly generated Rails engine in an app

I have a complex Rails app and I want to extract some core functionality into an engine so that I can reuse the models etc in other Rails apps.
I've been following the official documentation for engines (https://guides.rubyonrails.org/engines.html). I'm able to create a new engine inside the app and generate some test models
> rails plugin new testengine --mountable
testengine> rails generate model Test
This is the .gemspec
require_relative "lib/testengine/version"
Gem::Specification.new do |spec|
spec.name = "testengine"
spec.version = Testengine::VERSION
spec.authors = ["Me"]
spec.summary = "testengine"
# Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
# to allow pushing to a single host or delete this section to allow pushing to any host.
spec.metadata["allowed_push_host"] = "TODO: Set to 'http://mygemserver.com'"
spec.files = Dir["{app,config,db,lib}/**/*", "Rakefile", "README.md"]
spec.add_dependency "rails", "~> 6.1.4"
end
I console into the test dummy rails app in testengine, and I can find my new model at Testengine::Test, no problem. So far so good.
Now I get to section 4.1 Mounting the Engine. I add the engine via the Gemfile file (in fact this is already done for me thanks to the rails generator above).
gem 'testengine', path: 'testengine'
Then I install my gems without problems.
> bundle install
...
Using testengine 0.1.0 from source at `testengine`
...
I console into the main app and I can find Testengine and Testengine::VERSION but not Testengine::Engine or Testengine::Test.
Reading a little further the docs say you need add this line to config/routes.rb
mount Testengine::Engine, at: "/testengine"
I do and now the rails app won't even start
config/routes.rb:3:in `block in <top (required)>': uninitialized constant Testengine::Engine (NameError)
What did I miss?
I will answer my question for the benefit of others who might make the same mistake I made. In my case, gem 'testengine', path: 'testengine' was buried inside a group of gems e.g. in
group :test do
...
end
I guess I was confused how rails loads gems from groups and missed the detail about group inclusion. It seems that while it'll show in the list during bundle install, and autoload some basic items, such as Testengine::VERSION it doesn't autoload everything unless you are running an environment as the same name as the group. In hindsight, this seems a bit obvious. Lesson learned.

What can be done to fix the following error when we run any rails command: " `require_relative': cannot load such file "

Any rails command doesn't work for me. I have several versions of ruby installed through rvm. I tried installing rails with all the versions, they do install successfully but with all of them I face the following error whenever I run any rails command in my project directory:
~ rails new blog
Traceback (most recent call last):
1: from bin/rails:3:in `<main>'
bin/rails:3:in `require_relative': cannot load such file -- /Users/Am33d/Documents/config/boot (LoadError)
I tried looking up for the error but didn't find any solutions.
What can be done to fix this? I am using macOS Mojave (10.14.6)
This error would indicate that you do not have a boot.rb file in your config directory for some reason. When running rails commands -- regardless of if you run them as bin/rails [command] or bundle exec rails [command], runs the bin/rails file. This file typically has a line require_relative '../config/boot. The boilerplate bin/rails file in a new Rails 6 app is:
#!/usr/bin/env ruby
begin
load File.expand_path('../spring', __FILE__)
rescue LoadError => e
raise unless e.message.include?('spring')
end
APP_PATH = File.expand_path('../config/application', __dir__)
require_relative '../config/boot'
require 'rails/commands'
To simply fix this you can create a blank file by running touch config/boot.rb from the root directory of your application and that would itself suppress the error. Having said that, you'd be better off creating a config/boot.rb file that contains useful information. The boilerplate config/boot.rb file is this
ENV['BUNDLE_GEMFILE'] ||= File.expand_path('../Gemfile', __dir__)
require 'bundler/setup' # Set up gems listed in the Gemfile.
require 'bootsnap/setup' # Speed up boot time by caching expensive operations.
Without this file you are not necessarily appropriately loading the gems from your gemfile or caching operations with bootsnap.
When I ran into this problem, I also received the same error when trying to use rails -s.
If the same is happening for you, its because your version of ruby isn't compatible with your rails version.
After upgrading to the latest rails version the error stopped and everything worked well.
A little bit weird.
rails new blog should not need to find boot file, neither need bundler. Actually, you are trying to create a new project, so it is expected there is not Gemfile, boot, or bundler created for your project.
So I would advise you to find what rails command is being executed. Sometimes one rails executable created by bundler is taking precedence in the $PATH environment variable.
Ex: some incorrect bin/rails executable is on your $HOME directory.
i was trying to build an app using 'rails server' and this error was showing. You have to make sure that the rails version matches the ruby version and even if you do what an answer above said (create the config/boot.rb paste) doesnt work, than you have to change the version of rails to the stable. I'll let the link of this problem here, there's an issue closed in github for this problem https://github.com/rails/rails/pull/43951
to solve the problem you have to replace the gem rails line on the gemfile to this:
gem "rails", github: "rails/rails", branch: "7-0-stable"
Sorry about my english.

Best options for deploying a Ruby standalone script and dependencies?

for a Ruby standalone script what Rails like deployment features such as Gemfile / "bundle install" etc
that is assuming you are developing a Ruby script that you want to test and then deployment, and perhaps ship to others, what Rails like deployment approach would you use for say:
a) GEM - marking GEM requirements & having them installed as required - e.g. Rails "Gemfile" where you mark what gems you need and then "bundle install" to install them
b) File Require - automatically loading *.rb files if they are in your script directory (I'm thinking of in Rails where if you put a class file in the apps/model directory Rails automatically load/require's the file for you)
depends on whether it's a tool you expect people to use on every host they find it on or not. also depends on whether the tool can be shared with pubic repos.
if it just has to work, without worrying about whether you've installed the gems via bundler already or not, you can use something like the following from within your standalone script to install gems if not already present (be mindful of system vs. user ruby):
#!/usr/bin/env ruby
require 'rubygems'
def install_gem(name, version=Gem::Requirement.default)
begin
gem name, version
rescue LoadError
print "ruby gem '#{name}' not found, " <<
"would you like to install it (y/N)? : "
answer = gets
if answer[0].downcase.include? "y"
Gem.install name, version
else
exit(1)
end
end
end
# any of the following will work...
install_gem 'activesupport'
install_gem 'activesupport', '= 4.2.5'
install_gem 'activesupport', '~> 4.2.5'
require 'active_support/all'
...
In my humble opinion, a gem is the way to go. Bundler makes it easy to get started; it starts a skeleton for you when you run the command…
bundle gem <GEM_NAME>
Take a look. As long as you specify your dependencies in your gem's .gemspec file, and somebody installs your packaged gem (they won't need bundler, just RubyGems' gem command), the dependencies will be installed as gems along with it.

"uninitialized constant Etc" when running Ruby application

A few applications on my server relying on Ruby and Ruby On Rails seem to have stopped working. Or atleast partially so. I managed to get the web gui of the application in question to start functioning again by reinstalling different part of Ruby. But I most likley broke something else in the process.
I have managed to track it down using some tests and it seems like Etc is somehow not found by the program when it runs.
I tested by
ruby -e 'puts Etc.getpwnam("apache").uid'
and got
uninitialized constant Etc
If I instead tested
ruby -r etc -e 'puts Etc.getpwnam("apache").uid'
I get the correct answer
48
This is running on an Amahi6 server (running Fedora Core 14 64bit).
Example on the code in question in the actual application that fails is
#!/usr/bin/env ruby
# Amahi Home Server
# Copyright (C) 2007-2010 Amahi
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License v3
# (29 June 2007), as published in the COPYING file.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# file COPYING for more details.
#
# You should have received a copy of the GNU General Public
# License along with this program; if not, write to the Amahi
# team at http://www.amahi.org/ under "Contact Us."
require File.dirname(__FILE__) + '/../config/boot'
#require 'commands/runner'
require 'optparse'
# switch to apache:users first
uid = Etc.getpwnam("apache").uid
gid = Etc.getgrnam("users").gid
Process.gid = gid
Process.egid = gid
Process.uid = uid
Process.euid = uid
...
this fails with the same
uninitialized constant Etc
Clarification: I have NOT changed this code at all and it has worked in it's current state before
What could it be that I have messed up? :)
UPDATE:
Ruby installed now is version 1.8.7
UPDATE 2:
If I try to manually add a require for rubygems and etc it fails, but complains about another package. So I think there is something fundamentally messed up with the core of my ruby.
/usr/lib64/ruby/gems/1.8/gems/activesupport-2.3.4/lib/active_support/dependencies.rb:55:
uninitialized constant ActiveSupport::Dependencies::Mutex (NameError)
from
/usr/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:36:in
gem_original_require'
from
/usr/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:36:inrequire'
from
/usr/lib64/ruby/gems/1.8/gems/activesupport-2.3.4/lib/active_support.rb:56
from
/usr/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:36:in
gem_original_require'
from
/usr/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:36:inrequire'
from
/usr/lib64/ruby/gems/1.8/gems/activerecord-2.3.4/lib/active_record.rb:25
from
/usr/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:36:in
gem_original_require'
from
/usr/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:36:inrequire'
from /var/hda/platform/html/script/install-app:70
Ended up being that the versions of rails and gems were to new. Downgraded to gems 1.3.7 and rails 2.3.8.
gem update --system 1.3.7
gem uninstall rails
gem install rails --version 2.3.8
You seem to be missing require 'rubygems' and require 'etc' (put them below require 'optparse')
remember about 'rubygems',
commandline:
ruby -rubygems -e 'puts Etc.getpwnam("apache").uid'
script:
require 'rubygems'
puts Etc.getpwnam("apache").uid

Rails 3, Bundler, LoadError

I'm writing an app that will run scripts in a specified folder, and then record the numbers and graph them.
My problem is that if the script is a ruby file, the require statements fail inside the script because bundler seems to have done something funky with the load path.
Running rails runner Datasource.run_jobs fails:
class Datasource < ActiveRecord::Base
def self.run_jobs
require 'aws_sdb'
access_key_id = "REDACTED"
secret_key = "REDACTED" # In all actuality these woudln't be here.
#sdb = AwsSdb::Service.new(:access_key_id => access_key_id, :secret_access_key => secret_key)
Datasource.all.each do |ds|
puts "Updating #{ds.name}..."
unless #sdb.list_domains.include? ds.name
puts "#{ds.name} doesn't exist in SDB, creating a domain for it..."
#sdb.create_domain ds.name
end
#data = "TEST"
data = `#{RAILS_ROOT}/lib/plugins/#{ds.name}`
#sdb.put_attributes(ds.name, Time.now.to_i, data)
puts "Data Collected: #{data.inspect}"
end
end
has_many :graphs
end
(Basing this off your comments on the question)
You will need to add hpricot (and any other gem this needs) to your Gemfile so that they are made available by Bundler. Bundler is by far the easiest way to avoid gem conflicts and tomfoolery.
Imagine this situation: You somehow lose the gems that you have currently. Be this happening through a format or system change or any other reason. Whatever it is, you've lost your gems. How are you going to re-install all your gems? You could keep a list of them somewhere else yourself, but is this truly likely?
Bundler solves this problem by making you state what gems your application requires and only adding those gems to the load path, which is why you can't find hpricot. When you run bundle install the first time, this creates a Gemfile.lock which contains something like this:
GEM
remote: http://rubygems.org/
specs:
abstract (1.0.0)
actionmailer (3.0.0)
...
Because you commit this file to your source control "solution" of choice (be it Git, SVN, FTP, whatever, it's not important) you have a solid way of specifying the precise gems and precise versions of those gems that your application uses.
When/If your gems are wiped, you can simply clone your project again and run bundle install. Because the Gemfile.lock file exists, you'll have exactly the same gems you had originally, even if there were updates.
If you don't want the exact same gems, just run bundle update and this will ignore the specifications in Gemfile.lock and instead revert to depending on Gemfile to define them. This will check for new versions of gems and install them, updating the Gemfile.lock when it's done.
Honestly, I don't understand the Bundler hate. If you could explain in wider terms than "OMG IT SUCKS YEHUDA IS SATAN", I'd be much obliged.
Edit: WedTM asked for a sample Gemfile and related code:
In the Gemfile you'd have this:
group(:scripts) do
gem 'gem1'
end
To require these gems for your scripts:
require 'bundler'
Bundler.require(:scripts)
You may also wish to require the default gems too which you can do by just adding default anywhere to the arguments of require:
Bundler.require(:default, :scripts)
If this for some reason doesn't work I would imagine it would be because it can't locate the Gemfile. This can be fixed by setting the ENV['BUNDLE_GEMFILE'] to the path to the Gemfile.
I wonder if you might be able to use RVM to set up the ruby environment before running your scripts. Maybe something with a gemset like:
data = `rvm gemset use scripts; #{RAILS_ROOT}/lib/plugins/#{ds.name}`

Resources