Why is too hard to create a simple proyect with rails? - ruby-on-rails

Only i want to create a simple proyect with ruby and rails, but i think its harder than it looked.
I have a version of ruby 2.6.8, i created a new project with this command rails new blog, after that cd to blog
i applied this command: bin/rails server
i am facing this error: bin/rails:4:in require_relative': cannot load such file -- .../rb/blog/config/boot (LoadError) from bin/rails:4:in '

check with command:
ruby -v
that you ruby version same as in gemfile file, also as mention before don't forget run "bundle" after creating new project or add new gem to gemfile.
If this didn't help and you rails version is 6 you can manually fix problem - create boilerplate config/boot.rb:
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.
i am only start learning rails but i think if you only beginner then try fix problem in another way such reinstall rails, ruby etc. bc in future it can cause problem, its not normal when you just create new project and it already dont work, something wrong.

Related

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.

Ruby on Rails: Best way to add gems at project initialization

Every month or so I have to create a Rails project at work.
The procedure is always the same...
Run rails new [...] & edit the Gemfile.
I have about 12 gems I always add on any project no matter what kind of project that is. Most of them are development gems, some of them production gems.
The gems range from ActiveAdmin to BetterErrors and so on. Problem is: I always have to remember which gems I use on a regular basis and run the same procedure everytime I start a project:
Open google.com
Search for the correct names of the gems
Add the gems to the Gemfile accordingly
Repeat til I'm satisfied with my Gemfile
I wish I could do something like this: rails new Project --use-my-gems
I know it's possible somehow using a text-file to store my most used gems (or a Gemfile itself and overwrite the default Gemfile with my).
Basically I want multiple Rails Gemtemplates which can be switched at project initialization by passing an argument to rails.
Use Rails Templates.
You can specify your gems in the template:
gem "bj"
gem "nokogiri"
... with a lot of other options.
Then start your new app with:
rails new app -m ~/template.rb

Calling a ruby script from Rails with different gemfile

I have a ruby script I wrote which generates data and loads it to MongoDB. I am now trying to call this load script from seed.rb of my Rails app (so I can run via rake db:seed)
I attempted to call it from rails using this code:
system( "ruby data_load/db_load.rb -a data_load/doc1.json" )
When that code executes, I get the following error. (Note it runs fine from the command line):
data_load/db_load.rb:15:in `require': cannot load such file -- mongo (LoadError)
from data_load/db_load.rb:15:in `<main>'
The top of db_load.rb looks like this:
# includes gems from gemfile
require 'rubygems'
require 'bundler/setup'
Bundler.setup
require 'mongo'
require_relative 'load_scripts/cmd_options'
require_relative 'load_scripts/build_index'
....
include Mongo
The script has it's own gemfile in the data_load directory.
My guess is ruby is running the script using the bundle for the rails application instead of the shell script.
Any suggestions on how I can execute this script?
I believe the problem is where Bundler is looking for the Gemfile. Since your script is being run in the parent directly it is finding the Gemfile for the main app.
Set the BUNDLE_GEMFILE before calling your script:
system "BUNDLE_GEMFILE=data_load/Gemfile ruby data_load/db_load.rb -a data_load/doc1.json"
I'm sorry but I think you can't do it, unless you run the script as a different process (like a shell command), doing it is easy:
`shell_command params`
Just use the correct path and params.
Otherwise, consider that a gemfile is "more or less" at its basic level, a bunch of require (or load) statements, so loading a different gemfile would overwrite the original one, creating a lot of issue with rails after that.
The subprocess command is a good idea, however you can only pass string as params, not in-memory objects.

Trouble including httparty in ruby on rails

I've been trying to use HTTParty in my rails code
sudo gem install httparty
From the command line I can now successfully do
httparty "http://twitter.com/statuses/public_timeline.json"
When I try this in my rails app
require 'rubygems'
require 'httparty'
class FooController < ApplicationController
include HTTParty
def bar
blah = HTTParty.get("http://twitter.com/statuses/public_timeline.json")
end
end
I get the error message "no such file to load -- httparty"
I suspect there is something wrong with my environment?
You don't need to do 'include HTTParty' inside the Controller. Just remove that and it should work. I just tested it and it worked for me. If this doesn't work for you, you should add the gem to your environment.
Usually if you use a gem inside your Rails application, you should add the following to environment.rb:
config.gem "httparty"
The gem will be available in the application now and you don't need to add 'require' inside the Controller. Also, you don't need to require RubyGems inside a Controller.
When you use Rails 3, you need to put the following inside the Gemfile:
gem "httparty"
I hope it works for you. :)
The problem is, if you load a new gem, you have to restart the server even if you are in development.
I had this same error. I tried moving the require HTTParty all over, but found, all I needed to do was restart the rails server In the end I did not need to 'require HTTParty' nor 'include' it. It just needed to be loaded into rails.
1)include the httpary in your gemfile
open your gem file then add
gem 'httparty','YOUR VERSION NUMBER'
2) run bundle install in your command prompt of the app file
3) restart the server
Ran into the same problem. Then I switched from Ruby 1.8.7 to Ruby 1.9.2 and all errors varnished into thin air.
(Yes, it first took me quite some hours to come up with the possibility that the Ruby version might be the problem. Configured a secundairy server to avoid possible conflicts with 2 ruby versions, and after way to many hours I got my RoR stack up and running. And the first test with httparty (based on the example on top) worked out of the box! Finally can sleep RESTfully again :-)
I run into the same error whilst reviewing a project from a student, I change the name of the Gem from uppercase to lowercase then run bundle install. I then went ahead to change the format in which they were being imported from
require 'HTTParty' to require 'httparty' and boom it worked

When -exactly- does the Rails3 application get initialized?

I've been fighting left and right with rails 3 and bundler. There are a few gems out there that don't work properly if the rails application hasn't been loaded yet. factory_girl and shoulda are both examples, even on the rails3 branch.
Taking shoulda as an example, when trying to run rake test:units I get the following error:
DEPRECATION WARNING: RAILS_ROOT is deprecated! Use Rails.root instead. (called from autoload_macros at c:/code/test_harness/vendor/windows_gems/gems/shoulda-2.10.3/lib/shoulda/autoload_macros.rb:40)
c:/code/test_harness/vendor/windows_gems/gems/shoulda-2.10.3/lib/shoulda/autoload_macros.rb:44:in 'join': can't convert #<Class:0x232b7c0> into String (TypeError)
from c:/code/test_harness/vendor/windows_gems/gems/shoulda-2.10.3/lib/shoulda/autoload_macros.rb:44:in 'block in autoload_macros'
from c:/code/test_harness/vendor/windows_gems/gems/shoulda-2.10.3/lib/shoulda/autoload_macros.rb:44:in 'map'
from c:/code/test_harness/vendor/windows_gems/gems/shoulda-2.10.3/lib/shoulda/autoload_macros.rb:44:in 'autoload_macros'
from c:/code/test_harness/vendor/windows_gems/gems/shoulda-2.10.3/lib/shoulda/rails.rb:17:in '<top (required)>'
Digging a bit deeper into lib/shoulda/rails, I see this:
root = if defined?(Rails.root) && Rails.root
Rails.root
else
RAILS_ROOT
end
# load in the 3rd party macros from vendorized plugins and gems
Shoulda.autoload_macros root, File.join("vendor", "{plugins,gems}", "*")
So...what's happening here is while Rails.root is defined, Rails.root == nil, so RAILS_ROOT is used, and RAILS_ROOT==nil, which is then being passed on to Shoulda.autoload_macros. Obviously the rails app has yet to be initialized. With Rails3 using Bundler now, there's been some hubub over on the Bundler side about being able to specify an order in which the gems are required, but I'm not sure whether or not this would solve the problem at hand.
Ultimately my questions is this: When exactly does the environment.rb file (which actually initializes the application) get pulled in? Is there any harm to bumping up when the app is initialized and have it happen before the Bundler.require line in config/application.rb? I've tried to hack bundler to specify the order myself, and have the rails gem pulled in first, but it doesn't appear to me that requiring the rails gem actually initializes the application.
As this line (in config/application.rb) is being called before the app is initialized, any gem in the bundler Gemfile that requires rails to be initialized is going to tank.
# Auto-require default libraries and those for the current Rails environment.
Bundler.require :default, Rails.env
Well, it was actually fairly easy to trace this down. All the rails libraries are being pulled in in application.rb. The app itself is being initialized in environment.rb.

Resources