I wanted my seeds.rb file to have two paths based on some user input. For the sake of simplicity in this question I've stripped it down to just these two lines:
print "> "
res = gets.chomp
When I run rake db:seed, the following exception is raised:
▶ rake db:seed
> rake aborted!
Errno::ENOENT: No such file or directory # rb_sysopen - db:seed
/home/me/work/my_app/db/seeds.rb:5:in `gets'
/home/me/work/my_app/db/seeds.rb:5:in `gets'
/home/me/work/my_app/db/seeds.rb:5:in `<top (required)>'
Anyone know why this is happening, i.e. why gets.chomp in this context is causing the program to try to open a file named db:seed?
Try using STDIN.gets.chomp instead of gets.chomp.
See What's the difference between gets.chomp() vs. STDIN.gets.chomp()? for the explanation.
Related
I was trying to run the following command but the result was not what i expected
rake db:create
the result:
rake aborted!
LoadError: cannot load such file -- /mnt/c/newapp/config/application
/mnt/c/newapp/rakefile:4:in require_relative' /mnt/c/newapp/rakefile:4:in <top (required)>'
(See full trace by running task with --trace)
i tried reinstalling postgres but that did not solve the problem, I also tried to install all ruby's packages but it did not work at all.
The error is telling you that on line 4 of your rakefile the require_relative "config/application" line is failing, "cannot load such file -- /mnt/c/newapp/config/application", do you somehow not have a config/application.rb file in your project?
I was creating a quick one-off task:
require 'yaml'
task generate_permissions_yaml: :environment do
permissions = []
Permission.order(:title).each do |permission|
permissions << {
title: permission.title,
code: permission.code,
description: permission.description
}
end
puts permissions.to_yaml
end
When I ran it with rails generate_permissions_yaml, I got this error:
rails aborted!
Don't know how to build task 'generate_permissions_yaml' (see --tasks)
/bundle/gems/railties-5.0.2/lib/rails/commands/rake_proxy.rb:14:in `block in run_rake_task'
/bundle/gems/railties-5.0.2/lib/rails/commands/rake_proxy.rb:11:in `run_rake_task'
/bundle/gems/railties-5.0.2/lib/rails/commands/commands_tasks.rb:51:in `run_command!'
/bundle/gems/railties-5.0.2/lib/rails/commands.rb:18:in `<top (required)>'
As it turns out, it was due to the way that I named the file. It should have a .rake extension, not .rb.
I renamed this:
lib/tasks/generate_permissions_yaml.rb
To this:
lib/tasks/generate_permissions_yaml.rake
I uncovered this after trying to run rails g task generate_permissions_yaml and seeing that it generated a file with a .rake extension.
For newbies - this is a common error if you use a space instead of a colon in a rake command. For example if you enter:
rake db migrate
instead of:
rake db:migrate
The correct format has a colon, not a space
I'm trying to clone the Catarse repository: Git catarse
and I get all the way to the end but once I try to run rake db:seed I get the following error message:
rake aborted!
Errno::ENOENT: No such file or directory # dir_initialize - /Users/'USERNAME'/Desktop/my_websites/crowdfund_test/catarse/tmp/cache/
/Users/'USERNAME'/Desktop/my_websites/crowdfund_test/catarse/db/seeds.rb:116:in `<top (required)>'
Tasks: TOP => db:seed
Do you know why??
I was having the same problem. Then I realized I did not have the directory
/yourPath/catarse/tmp/cache/
Once I created it, seeding worked just fine!
*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.
Although this might sound similar to the other questions you find here, there is a slight twist. I have two directories, say /home/rails/Rake and /home/rails/test_app. The rails directory is where I place all my rails projects.
Inside Rake, I have a Rakefile and a create.rake file.
This is what my rakefile look's like
namespace :setup do
desc "something"
task :init do
print "Name of the destination directory: "
name = STDIN.gets.strip
cp_r '.', "../#{name}/lib/tasks"
cd "../#{name}"
sh "rake setup:create"
end
end
And create.rake
namespace :setup do
desc "Install"
task :create do
sh 'git init'
#some other code
end
end
What it does is obvious. I want to copy the contents of the Rake directory to /test_app/lib/tasks. Then change directory to test_app and run setup:create task defined in the install.rake file now placed in test_app/lib/tasks. This works, but is this the rake way of doing it? Can anyone give me a slight hint of how it's done, the Rake way.
Here is the error which I get when I used invoke method:
$ rake setup:init
Name of the destination directory:
testapp
cp -r . ../testapp/lib/tasks
cd ../testapp
rake aborted!
Don't know how to build task 'setup:create'
/home/TradeRaider/rails/Rake/Rakefile:8:in `block (2 levels) in <top (required)>'
/home/TradeRaider/.rvm/gems/ruby-2.0.0-p247/bin/ruby_noexec_wrapper:14:in `eval'
/home/TradeRaider/.rvm/gems/ruby-2.0.0-p247/bin/ruby_noexec_wrapper:14:in `<main>'
Tasks: TOP => setup:init
(See full trace by running task with --trace)
This is more rake-ish :)
Rake::Task["setup:create"].invoke
Although #apneadiving answer helped, it just struck me that I was trying to call a Rakefile from another Rakefile, literally speaking. Anyways, to do so, I had to first load the rake file,
load "../#{name}/lib/tasks/create.rake"
(requiring it will also do the trick)
and then invoke it.
Rake::Task["setup:create"].invoke