Rails console not working on server - ruby

When I run bundle exec rails console production or rails console production via SSH on the server in the Current folder of the Capistrano deploy I get:
Usage:
rails new APP_PATH [options]
Options:
(...)
with an explanation to start a new app. Locally it works. Why can't I start a console remotely?

I'm assuming that you updated to rails 4 from version 3 and your app can't find the executables in the bin directory. Run this to see your rails version:
$ rails -v
If your rails version is 4 or above, try running this:
$ rake rails:update:bin
Source: Rails 4 Release Notes
6.1 Notable changes
Your app's executables now live in the bin/ dir. Run rake rails:update:bin to get bin/bundle, bin/rails, and bin/rake.

I am using capistrano to deploy, including the capistrano/bundler gem. Since the ./bin directory is version controlled in Rails 4, we need to prevent Capistrano from linking it on deployments by removing bin from set :linked_dirs.
Now in order to prevent bundler from overwriting the version controlled binstubs, we can add the line set :bundle_binstubs, nil which will prevent capistrano-bundler from setting the --binstubs option when running bundle install.
My config/deploy.rb file now has these lines:
# Default value for linked_dirs is []
set :linked_dirs, fetch(:linked_dirs, []).push('log', 'tmp/pids', 'tmp/cache', 'tmp/sockets', 'vendor/bundle', 'public/system')
set :bundle_binstubs, nil
Note the lack of the bin directory in the :linked_dirs line.

I have the same problem, and turns out when you deploy through cap shared/bin is symlink to current/bin.
Here's what works for me:
rm current/bin
mkdir current/bin
rake rails:update:bin
This should help, but it is somewhat a temporary solution, I'm trying to find out how to make cap not auto symlink-ing current/bin.

In case of Rails 5.2
I had to remove bin directory by running below command in project root directory.
rm -rf bin
and then I ran another command in project root directory:
rake app:update:bin
It will show you output like below:
create bin
create bin/bundle
create bin/rails
create bin/rake
create bin/setup
create bin/update
create bin/yarn
That's it.

It's been a little while since this was answered.
In my case I needed to run:
rake app:update:bin
Note- app rather than rails.
I was missing the bin directory all-together in my Rails 5.1 App

I faced this issue on my production server when I updated my application from Rails 5.2 to 6.0.4. Simply follow these steps:
cd to_your_project_dir
rm -r bin
rake app:update:bin

Related

Ruby Native Extension - Manual Compilation

I created new Ruby C Extension and hosted it in GitHub,
and I install via Bundler (gem 'my_cool_gem', git: '..').
But when I run the application (rails s) I get an error -
cannot load such file -- my_cool_gem/my_cool_gem
My current solution, is to manually compile it:
$ cd $(bundle show my_cool_gem)
$ rm ext/my_cool_gem/*.o
$ rake compile
only then, my app works.
Any ideas?
You probably misconfigured your extconf.rb.
Make sure you have create_makefile line to look like:
create_makefile(File.join(extension_name, extension_name))

How can I restore my Rails 5 binstubs?

I'm working in a Rails 5 app on macOS Sierra and everything was going well until I was ready for production to a Digital Ocean VPS. I followed one of the most famous Deploy Rails app tutorial using Capistrano, I after some bugs finally my app came to live running on production.
Now in my local environment when I run rails server or rails console I got this warning and I don't know how to fix it or whats going wrong with that.
Looks like your app's ./bin/rails is a stub that was generated by Bundler.
In Rails 5, your app's bin/ directory contains executables that are versioned
like any other source code, rather than stubs that are generated on demand.
Here's how to upgrade:
bundle config --delete bin # Turn off Bundler's stub generator
rails app:update:bin # Use the new Rails 5 executables
git add bin # Add bin/ to source control
You may need to remove bin/ from your .gitignore as well.
When you install a gem whose executable you want to use in your app,
generate it and add it to source control:
bundle binstubs some-gem-name
git add bin/new-executable
=> Booting Puma
=> Rails 5.0.0.1 application starting in development on http://localhost:3000
=> Run `rails server -h` for more startup options
Puma starting in single mode...
* Version 3.6.0 (ruby 2.3.1-p112), codename: Sleepy Sunday Serenity
* Min threads: 5, max threads: 5
* Environment: development
* Listening on tcp://localhost:3000
Use Ctrl-C to stop
I have been searching whats this bug and how to fix it but I have no luck! And for reference it seems to be something with Capistrano but I have done what they said is the solution and it does not work or maybe I have not implement it in the right way:
The solution would be:
1.- remove bin from the linked_dirs (which was not my case)
2.- add set :bundle_binstubs, nil to your config/deploy.rb to generate the binstubs
Some articles I found:
https://github.com/capistrano/rails/issues/171
https://github.com/capistrano/capistrano/issues/1675
Rails 5 console not working when deploying with Capistrano
https://github.com/capistrano/bundler/issues/45
I really appreciate any type of help you could provide me.
Thanks in advance.
I know this comes a bit late, but I found that I could run $ rails app:update and it would restore/overwrite my binstubs... along with overwriting all the other configuration files you start out with. So be careful about that.
See the Upgrading Ruby on Rails guide for more on this task.

task in Capistrano 3 cannot find RVM

I have been using Capistrano 3 to deploy a PHP app for several months and it works great. Recently, we decided to start using Sass for stylesheets and I am now trying to deploy these changes.
I am trying to write a task that runs after the rest of the deploy stuff is finished that converts a scss file to css using the Sass gem.
namespace :deploy do
after :finished, :assets do
on roles(:app), in: :sequence, wait: 5 do
within release_path do
# process sass files to css
execute "sass #{release_path}/styles/test.scss #{release_path}/styles/test.css"
end
end
end
end
I am using RVM on the server and have the sass gem installed in a specific gemset. There is also an .rvmrc file in the project root that loads the correct gemset when you cd into the 'current' directory that capistrano creates.
When I deploy, it fails on my new task saying that it cannot find sass.
stderr: bash: sass: command not found
I can log into the server as the same user that is used to deploy with Capistrano and cd into the 'current' directory and run the same command in the task (substituting the #{release_path} with actual path) and it works fine.
Things I have tried:
rewriting the execute command with the following:
rvm use 2.1.5#deployer && sass #{release_path}/styles/test.scss #{release_path}/styles/test.css
writing a bash script accessible by the deployer user that loads the gemset then runs the sass command (works when I run the new script while logged into the sever in any directory, does not work when called from the capistrano task)
using the capistrano-rvm plugin (adding to Gemfile, requiring in Capfile) to set the RVM gemset - hoping that it would load the gemset before running any commands.
I have used Capistrano many times for deploying Rails apps and always use the asset plugin that handles precompiling and whatnot... This is the first project that I have used for deploying a PHP app and maybe the first time I have tried to manually run a capistrano task that uses a rubygem installed on the server with RVM.
Is it possible to run a task that depends on a specific gem/gemset... without using the default rails plugins?
Any help is appreciated.
Thanks,
JD
figured this one out by requiring the capistrano/bundler command, just to see how capistrano runs bundle by default... copied the bundler command that was logged during a deploy, then modified it to work for what I was trying to do and finally removed the bundler plugin as I do not actually need the deploy to bundle anything.
without using any capistrano plugins, you can preface the capistrano task execute command with the location of the gemset like so:
after :finished, :assets do
on roles(:app), in: :sequence, wait: 5 do
within release_path do
execute "~/.rvm/bin/rvm ruby-2.1.5#deployer do sass #{release_path}/styles/sass/screen.scss #{release_path}/styles/screen.css"
end
end
end
The key difference is the following snippet prefaces the actual command I was trying initially:
~/.rvm/bin/rvm ruby-2.1.5#deployer do ....
Obviously, you could use whatever command requiring the gemset specified that you want (instead of the sass command I am using).

How to build and run a Ruby command-line tool found on github?

Hi cloned a simple app ( https://github.com/cfx/twix) on github that allows me to send Twitter messages from the console, but I'm not sure how to run it.
I now have a folder in my users/name director called Twix. Inside twix, I have these folders created by the app.
README Rakefile bin lib test
The readme doesn't provide a lot of instruction to get things started. It just tells you what to do after the program's running (see below).
Questions: What command do I use to get this started? What folder do I need to be in?
The README
Twix 0.0.1
Simple twitter client for console
Keys:
q - quit
t - write new message
If you want to post your twit, finish you message with !SEND
If you want abort and back to your feed, finish your message with !EXIT
new features soon
This program is packaged as a gem. The following command will install the gem (run it in the Twix directory):
rake package && gem install pkg/twix-0.0.1.gem
You can now run the program from anywhere with the twix command.
Edit
The first thing I saw in the repository was the Rakefile, meaning there were some rake tasks defined. When you find yourself in this case, running rake -T is your best bet as it will show you the rake tasks available. Its output was the following:
(in /home/benoit/code/clones/twix)
rake clobber_package # Remove package products
rake gem # Build the gem file twix-0.0.1.gem
rake package # Build all the packages
rake repackage # Force a rebuild of the package files
I picked the command that would be the most likely to build the gem: the package one. I guess the gem task would have worked as well.
Running rake package gave me the following output:
(in /home/benoit/code/clones/twix)
mkdir -p pkg
WARNING: no homepage specified
Successfully built RubyGem
Name: twix
Version: 0.0.1
File: twix-0.0.1.gem
mv twix-0.0.1.gem pkg/twix-0.0.1.gem
All that was left was to install the pkg/twix-0.0.1.gem created by the previous command:
gem install pkg/twix-0.0.1.gem
There's another case you could have encountered: the presence of a twix.gemspec in the root directory of the application. In this case, running:
gem build twix.gemspec
would have built the gem, the installation step remains the same.
ruby ./bin/twix
or
ruby -I lib ./bin/twix

what is the ideal directory structure of a ruby application

what is the ideal directory structure of a ruby application.
I want it to be deployed on EC2. It should have Gemfile and Rake file for executing rake tasks.
It should contain a lib file for utilities.
Should I just be using the command
newgem --simple test
Take a look at this RailsCast.
'bundle gem simple_test' works great for me.
$ bundle gem simple_test
create simple_test/Gemfile
create simple_test/Rakefile
create simple_test/.gitignore
create simple_test/simple_test.gemspec
create simple_test/lib/simple_test.rb
create simple_test/lib/simple_test/version.rb
Initializating git repo in /Users/sean/dev/foo/simple_test
$

Resources