Rails pathname missing in environment.rb - ruby-on-rails

When I'm trying to install Diaspora* and after my rake db:setup I get the following error when I try open it from the browser:
NameError: uninitialized constant Pathname
/Users/myself/diaspora/config/environment.rb:15:in `<top (required)>'
the line 15 is the first after the comment:
# Load the rails application
require Pathname.new(__FILE__).dirname.expand_path.join('application')
require Rails.root.join("lib", "exceptions")
I'm also using Pow.

Add require 'pathname' at the top of environment.rb to make this core Ruby library available.

Related

Why am I getting 'NameError: uninitialized constant Pry' when gem is installed properly?

I have installed pry version 0.10.4 on my application and I have delegated my task in my rake file to run Pry when I need to debug and check my code.
task :console do
Pry.start
end
But every time I type in rake console I get the error below:
rake aborted!
NameError: uninitialized constant Pry
/Users/kenkuts/Desktop/Projects/flatiron_school/sinatra-restful-routes-lab-cb-000/Rakefile:9:in block in <top (required)>'
/Users/kenkuts/.rvm/gems/ruby-2.5.0/gems/rake-12.3.2/exe/rake:27:in <top (required)>'
/Users/kenkuts/.rvm/gems/ruby-2.5.0/bin/ruby_executable_hooks:15:in eval'
/Users/kenkuts/.rvm/gems/ruby-2.5.0/bin/ruby_executable_hooks:15:in <main>'
Tasks: TOP => console
(See full trace by running task with --trace)
Rake tasks don't automatically load the gems - any that you use have to be explicitly required.
In your case, you can simply add the following:
require 'pry'
task :console do
Pry.start
end
You could also use the pry-rails gem, which will handle launching the Rails console with pry.
Two other quick tips:
in the Rails console, you can quickly switch to Pry with the eponymous pry command
there's a good (but old) read about incorporating Pry here: http://lucapette.me/pry-everywhere
Hope that helps - let me know if you've any questions.

Mongoid::Errors::NoSessionConfig in rails consolse

I clone rails app (our company project) and then install everything that related to mongo db and mongoid gem. Then I run the rake db:setup and then rails s. Its working fine, I can access all the sites and do everything. But I got problem in the rails console.
Whenever I do this:
rails c
then
User.first
I will get this error
Loading development environment (Rails 4.1.1) irb(main):001:0>
User.first Mongoid::Errors::NoSessionConfig: Problem: No
configuration could be found for a session named 'default'. Summary:
When attempting to create the new session, Mongoid could not find a
session configuration for the name: 'default'. This is necessary in
order to know the host, port, and options needed to connect.
Resolution: Double check your mongoid.yml to make sure under the
sessions key that a configuration exists for 'default'. If you have
set the configuration programatically, ensure that 'default' exists in
the configuration hash. from
/Users/rizalmuthi/.rbenv/versions/2.1.1/lib/ruby/gems/2.1.0/gems/mongoid-4.0.0.beta2/lib/mongoid/sessions/factory.rb:27:in
create' from
/Users/rizalmuthi/.rbenv/versions/2.1.1/lib/ruby/gems/2.1.0/gems/mongoid-4.0.0.beta2/lib/mongoid/sessions.rb:65:in
with_name' from
/Users/rizalmuthi/.rbenv/versions/2.1.1/lib/ruby/gems/2.1.0/gems/mongoid-4.0.0.beta2/lib/mongoid/sessions.rb:105:in
mongo_session' from
/Users/rizalmuthi/.rbenv/versions/2.1.1/lib/ruby/gems/2.1.0/gems/mongoid-4.0.0.beta2/lib/mongoid/sessions.rb:121:in
collection' from
/Users/rizalmuthi/.rbenv/versions/2.1.1/lib/ruby/gems/2.1.0/gems/mongoid-4.0.0.beta2/lib/mongoid/sessions/options.rb:161:in
method_missing' from
/Users/rizalmuthi/.rbenv/versions/2.1.1/lib/ruby/gems/2.1.0/gems/mongoid-4.0.0.beta2/lib/mongoid/contextual/mongo.rb:263:in
initialize' from
/Users/rizalmuthi/.rbenv/versions/2.1.1/lib/ruby/gems/2.1.0/gems/mongoid-4.0.0.beta2/lib/mongoid/contextual.rb:53:in
new' from
/Users/rizalmuthi/.rbenv/versions/2.1.1/lib/ruby/gems/2.1.0/gems/mongoid-4.0.0.beta2/lib/mongoid/contextual.rb:53:in
create_context' from
/Users/rizalmuthi/.rbenv/versions/2.1.1/lib/ruby/gems/2.1.0/gems/mongoid-4.0.0.beta2/lib/mongoid/contextual.rb:35:in
context' from
/Users/rizalmuthi/.rbenv/versions/2.1.1/lib/ruby/gems/2.1.0/gems/mongoid-4.0.0.beta2/lib/mongoid/contextual.rb:20:in
first' from
/Users/rizalmuthi/.rbenv/versions/2.1.1/lib/ruby/gems/2.1.0/gems/mongoid-4.0.0.beta2/lib/mongoid/findable.rb:122:in
first' from (irb):1 from
/Users/rizalmuthi/.rbenv/versions/2.1.1/lib/ruby/gems/2.1.0/gems/railties-4.1.1/lib/rails/commands/console.rb:90:in
start' from
/Users/rizalmuthi/.rbenv/versions/2.1.1/lib/ruby/gems/2.1.0/gems/railties-4.1.1/lib/rails/commands/console.rb:9:in
start' from
/Users/rizalmuthi/.rbenv/versions/2.1.1/lib/ruby/gems/2.1.0/gems/railties-4.1.1/lib/rails/commands/commands_tasks.rb:69:in
console' from
/Users/rizalmuthi/.rbenv/versions/2.1.1/lib/ruby/gems/2.1.0/gems/railties-4.1.1/lib/rails/commands/commands_tasks.rb:40:in
run_command!' from
/Users/rizalmuthi/.rbenv/versions/2.1.1/lib/ruby/gems/2.1.0/gems/railties-4.1.1/lib/rails/commands.rb:17:in
' from
/Users/rizalmuthi/.rbenv/versions/2.1.1/lib/ruby/gems/2.1.0/gems/activesupport-4.1.1/lib/active_support/dependencies.rb:247:in require' from
/Users/rizalmuthi/.rbenv/versions/2.1.1/lib/ruby/gems/2.1.0/gems/activesupport-4.1.1/lib/active_support/dependencies.rb:247:inblock in require' from
/Users/rizalmuthi/.rbenv/versions/2.1.1/lib/ruby/gems/2.1.0/gems/activesupport-4.1.1/lib/active_support/dependencies.rb:232:in load_dependency' from
/Users/rizalmuthi/.rbenv/versions/2.1.1/lib/ruby/gems/2.1.0/gems/activesupport-4.1.1/lib/active_support/dependencies.rb:247:inrequire' from
/Users/rizalmuthi/Documents/Sites/WORK/tapway/bin/rails:8:in <top
(required)>' from
/Users/rizalmuthi/.rbenv/versions/2.1.1/lib/ruby/gems/2.1.0/gems/activesupport-4.1.1/lib/active_support/dependencies.rb:241:inload' from
/Users/rizalmuthi/.rbenv/versions/2.1.1/lib/ruby/gems/2.1.0/gems/activesupport-4.1.1/lib/active_support/dependencies.rb:241:in block in load' from
/Users/rizalmuthi/.rbenv/versions/2.1.1/lib/ruby/gems/2.1.0/gems/activesupport-4.1.1/lib/active_support/dependencies.rb:232:inload_dependency' from
/Users/rizalmuthi/.rbenv/versions/2.1.1/lib/ruby/gems/2.1.0/gems/activesupport-4.1.1/lib/active_support/dependencies.rb:241:in load' from
/Users/rizalmuthi/.rbenv/versions/2.1.1/lib/ruby/2.1.0/rubygems/core_ext/kernel_require.rb:55:in
require' from
/Users/rizalmuthi/.rbenv/versions/2.1.1/lib/ruby/2.1.0/rubygems/core_ext/kernel_require.rb:55:in
require' from -e:1:in'irb(main):002:0>
And FYI, this is the mongoid.yml that I generated by run
rails g mongoid:config
mongoid.yml
development:
sessions:
default:
database: mongo_demo_development
hosts:
- localhost:27017
options:
options:
test:
sessions:
default:
database: mongo_demo_test
hosts:
- localhost:27017
options:
consistency: :strong
max_retries: 1
retry_interval: 0
I have been looking all over google and some blogs, could not figure it out how to fix this.
Besides that, we dont use the Rspec tho.
change your config/application.rb file:
replace it Bundler.require(*Rails.groups)
to it Bundler.require(*Rails.groups(assets: %w(development test)))
In the latest mongoid -5.0.0 version you can simply solve by placing the below code in application.rb
config.generators do |g|
g.orm :mongoid
end
In config/application.rb change
Bundler.require(*Rails.groups)
to:
Bundler.require(:default, Rails.env)
Rails is trying to eager load the group of gems by environment and the stock config/mongoid.yml file you get when you run rails g mongoid:config only gives you a test and development namespace. Even though you're only running development, it's looking at all the groups. If you add the production namespace to the yml file it will load but my answer above works without you needing to do that.
Just make sure that on application initialization you do the following (in Rails console)
Mongoid.load!("path/to/your/mongoid.yml")
For more details visit http://mongoid.org/en/mongoid/docs/installation.html
With MongoId version 5.x, You will change config in Rails application follow:
File: application.rb
# Add line.
config.generators do |g|
g.orm :mongoid
end
I was set it and it working in rails console.
See more at:
https://docs.mongodb.com/ecosystem/tutorial/mongoid-installation/#rails-applications
may be The problem is in the gem file, you did't mention the version of mongoid gem.
in gem file change
gem "mongoid"
to
gem "mongoid" , '~> 4.0.2'
and run bundle install
restart server and console.

How do I get a custom Rake task to run in Sinatra?

*I want to get a custom Rake task to run in my Sinatra app but I keep getting rake aborted!
Don't know how to build task 'greet'.
Here's the custom Rake task (greet.rake) for testing purpose:
task :greet do
puts "Hello!"
end
I've put the greet.rake in ./lib/tasks (Rails). I'm guessing Rake can't find the correct directory for the file.
How do I get a custom Rake task to run in Sinatra?
I'm using Ruby 2.0.0 and Sinatra 1.4.4.
UPDATE
The Rakefile now looks like this:
require "./app"
require "sinatra/activerecord/rake"
require "./lib/tasks"
When using:
rake greet
I get:
rake aborted!
cannot load such file -- ./lib/tasks
/Users/*/.rvm/gems/ruby-2.0.0-p247#global/gems/activesupport- 4.0.1/lib/active_support/dependencies.rb:229:in `block in require'
/Users/*/.rvm/gems/ruby-2.0.0-p247#global/gems/activesupport- 4.0.1/lib/active_support/dependencies.rb:214:in `load_dependency'
/Users/*/.rvm/gems/ruby-2.0.0-p247#global/gems/activesupport-4.0.1/lib/active_support/dependencies.rb:229:in `require'
/Users/*/Dropbox/Development/Sinatra/sinatra-mp-experiment/Rakefile:3:in `<top (required)>'
(See full trace by running task with --trace)
Create a Rakefile at your Sinatra app's top directory, require the file that contains this task you want to use and you should be good to go.
Edit:
A simple solution is changing your Rakefile to:
require "./app"
require "sinatra/activerecord/rake"
Dir.glob('lib/tasks/*.rake').each { |r| load r}
Now any .rake file under lib/tasks will be loaded.

Why would I get a JSON error when I try to rake db:migrate?

When I try to run rake db:migrate I keep getting this error:
rake aborted!
no such file to load -- JSON
Has anyone else come across this problem, if so how did you solve it?
I have tried a lot of different things already. I tried adding the gem 'json_pure' to the Gemfile after doing some googling. I also tried checking out to previous branches that I know I was not getting this error. Kind of stuck now. Let me know if anyone can help me out.
The error is actually not happening on my friend's mac. It is happening on Heroku and on my linux computer.
I had a play with irb:
irb(main):002:0> require 'JSON'
LoadError: no such file to load -- JSON
from <internal:lib/rubygems/custom_require>:29:in `require'
from <internal:lib/rubygems/custom_require>:29:in `require'
from (irb):2
from /home/mslade/rubygems1.9/bin/irb:12:in `<main>'
irb(main):003:0> require 'json'
=> true
So there is a 'json' module, but no 'JSON' module.
When a migration runs it pulls in most/all of your rails setup as well. If you have a require 'JSON' somewhere in your code that you added into a module that hasn't yet been tested.

whenever gem schedule.rb file: doesn't recognize RAILS_ROOT variable

In schedule.rb file, the statement:
require "#{RAILS_ROOT}/config/environment.rb"
every "10 10 2 * * *" do
command "mysqldump -u #{#db_username} -p#{#db_password} --single-transaction #{#db_name} > #{#backup_Path}/#{#db_name}.sql 2> log/error_crontab.log"
end
When i try to execute the whenever cmd from terminal, getting the following error:
config/schedule.rb:48:in `initialize': uninitialized constant Whenever::JobList::RAILS_ROOT (NameError)
from /usr/local/lib/ruby/gems/1.9.1/gems/whenever-0.7.0/lib/whenever/job_list.rb:19:in `instance_eval'
from /usr/local/lib/ruby/gems/1.9.1/gems/whenever-0.7.0/lib/whenever/job_list.rb:19:in `initialize'
from /usr/local/lib/ruby/gems/1.9.1/gems/whenever-0.7.0/lib/whenever.rb:16:in `new'
from /usr/local/lib/ruby/gems/1.9.1/gems/whenever-0.7.0/lib/whenever.rb:16:in `cron'
from /usr/local/lib/ruby/gems/1.9.1/gems/whenever-0.7.0/lib/whenever/command_line.rb:40:in `run'
from /usr/local/lib/ruby/gems/1.9.1/gems/whenever-0.7.0/lib/whenever/command_line.rb:7:in `execute'
from /usr/local/lib/ruby/gems/1.9.1/gems/whenever-0.7.0/bin/whenever:38:in `<top (required)>'
from /usr/local/bin/whenever:19:in `load'
from /usr/local/bin/whenever:19:in `<main>'
i am using the require statement to get the dynamic values from the form to schedule the job. Please help to solve this issue?
Note: i have seen the following stackoverflow queries:
How to detect Rails environment inside whenever
Following this thread to get dynamic values, but facing problem with require statement.
Rails - Whenever gem - Dynamic values
Ruby/Rails - Whenever gem - Loop cron tasks
config file in schedule.rb with Rails Whenever gem?
Whenever doesn't require or depend on Rails at all, so when it runs, RAILS_ROOT is not defined, however because whenever's schedule.rb is generally kept in /config/schedule.rb, we can make an assumption that it is in a rails project, and set our own RAILS_ROOT like this:
# in schedule.rb
RAILS_ROOT = File.dirname(__FILE__) + '/..'
Edit: in the case that you actually need Rails loaded, do this:
# in schedule.rb
# this will require config/environment and load your entire rails environment
require File.expand_path(File.dirname(__FILE__) + "/environment")
The whenever developer already answered this question, check this out https://github.com/javan/whenever/issues/81
Javan Whenever no longer attempts to load your Rails environment. However, it does automatically set a path variable to the directory whenever was executed from. This should work just the same:
set :output, "#{path}/log/cron.log"
In Rails 4 try with:
require File.expand_path(File.dirname(__FILE__) + "/../config/environment")
in your schedule.rb file.
This way you also have access to all your active-record models and initializers.

Resources