rails c wont work in app directory - ruby-on-rails

Im trying to reach the production env console. I think rbenv is messing with me.
When i run rails console production i get the old:
Usage:
rails new APP_PATH [options]
So i figure, it doesn't recognize my dir as an rails app because of the versions so I run. So the rails -v tells me:
deploy#webb-labb2:~/prognoser/current$ rails -v
Rails 5.1.0
Which is the wrong version of the app.
And the bundle exec rails -v command gives me:
deploy#webb-labb2:~/prognoser/current$ bundle exec rails -v
Rails 4.2.8
Which is the correct version. But the:
deploy#webb-labb2:~/prognoser/current$ bundle exec rails console production
Still gives me the old:
Usage:
rails new APP_PATH [options]
Options:
-r, [--ruby=PATH]

I ran
$ bundle exec rake rails:update:bin
that did the trick. And I had to add a boot file to the shared/config directory.

Related

Getting rails console to work with multiple Gemfiles

We are currently migrating from Rails 4 to 5, and have two Gemfiles (similar to how GitHub did it), Gemfile (Rails 4) and Gemfile_5 (Rails 5).
The following commands work as expected:
bundle exec rails s
=> Booting WEBrick
=> Rails 4.2.11.12 LTS application starting in development on http://localhost:3000
BUNDLE_GEMFILE=Gemfile_5 bundle exec rails s
=> Booting WEBrick
=> Rails 5.0.7.1 application starting in development on http://localhost:8000
bundle exec rspec spec/...
# runs specs using Rails 4 gemset
BUNDLE_GEMFILE=Gemfile_5 bundle exec rspec spec/...
# runs specs using Rails 5 gemset
bundle exec rails --version
Rails 4.2.11
BUNDLE_GEMFILE=Gemfile_5 bundle exec rails --version
Rails 5.0.7.1
And yet, when trying to run console or runner, it'll only use the Rails 4 gemset:
BUNDLE_GEMFILE=Gemfile_5 bundle exec rails c
Loading development environment (Rails 4.2.11.12 LTS)
BUNDLE_GEMFILE=Gemfile_5 bundle exec rails r "puts Rails.version"
4.2.11
I've tried restarting Spring, but that hasn't had any effect. What am I missing here?
The trick was to fully disable Spring, rather than just restart it:
DISABLE_SPRING=1 BUNDLE_GEMFILE=Gemfile_5 bundle exec rails console
Loading development environment (Rails 5.0.7.1)

Unable to run rails console/server in AWS Cloud9 CodeStar EB dev Ruby on Rails environment

I created my first environment with CodeStar and selected the Ruby on Rails w/ Elastic Beanstalk option. I'm using AWS Cloud9 for the IDE. I'd like to use the Preview option to view the impact of code changes prior to committing, and have looked through the docs at http://docs.aws.amazon.com/cloud9/latest/user-guide/app-preview.html, however I can't seem to get a server running in the development environment.
From within my environment directory in the Cloud9 terminal (path: /home/ec2-user/environment/env_name) I tried rails s -b $IP -p $PORT as documented for the previous non-AWS Cloud9, and also rails server and even rails console just to check. In each case I just get the help details for rails new:
$ rails s
Usage:
rails new APP_PATH [options]
Options:
-r, [--ruby=PATH] # Path to the Ruby binary of your choice
...etc...
What am I missing?
Per the discussion on this question, this behavior indicates that rails does not recognize that it is running in a rails directory so it thinks the only valid action is rails new. There were several suggested answers, but the one that worked for me was to run rake rails:update:bin (or rake app:update:bin for Rails 5).

Command: rails console, generate not recognize. Error: Command 'rails' not recognized Usage: rails COMMAND [ARGS]

When I go to rails folder (I'm inside my rails app folder) and type "rails c" or "rails g" it said:
Error: Command 'rails' not recognized
Usage: rails COMMAND [ARGS]
Usage: spring COMMAND [ARGS]
Commands for spring itself:
binstub Generate spring based binstubs. Use --all to generate a binstub for all known commands. Use --remove to revert.
help Print available commands.
server Explicitly start a Spring server in the foreground
status Show current status.
stop Stop all spring processes for this project.
Commands for your application:
rails Run a rails command. The following sub commands will use spring: console, runner, generate, destroy, test.
rake Runs the rake command
Error: Command 'rails' not recognized
Usage: rails COMMAND [ARGS]
The most common rails commands are:
generate Generate new code (short-cut alias: "g")
console Start the Rails console (short-cut alias: "c")
server Start the Rails server (short-cut alias: "s")
dbconsole Start a console for the database specified in config/database.yml
(short-cut alias: "db")
new Create a new Rails application. "rails new my_app" creates a
new application called MyApp in "./my_app"
In addition to those, there are:
destroy Undo code generated with "generate" (short-cut alias: "d")
plugin new Generates skeleton for developing a Rails plugin
runner Run a piece of code in the application environment (short-cut alias: "r")
All commands can be run with -h (or --help) for more information.
I'm running Linux Subsystem for Windows 10, I know I know you see Windows but it's kind of... Linux, and I installed Ruby by rbenv through this tutorial.
Try this command from your app folder:
rake rails:update:bin
Say y to replace the binaries.
I had the same problem using rails with Windows Linux Subsystem. I found the suggestion here:
Rails command Error
I tried it and it solved my problem, so maybe yours as well.
Even though the accepted answer solved your issue, I want to add another because I ran into the same error message but the underlying issue and my environment setup wasn't the same. However, recreating the executables in my app's bin folder did not solve the problem.
The issue: When I tried running bundle exec rails s or bundle exec rails c I got the same error as posted in the question. Using the apps executables worked fine though ./bin/rails s.
So the main differences from the question: I was using bundle exec when I got the error.
What was happening behind the scenes: bundle exec rails s was calling this executable /Users/robin/.rvm/gems/ruby-2.3.8/bin/rails which is shipped by the railties gem. I found this file was overwritten due to a gem which had an executable rails defined in its .gemspec.
What solved my problem:
Running $ bundle pristine railties (https://bundler.io/v2.0/man/bundle-pristine.1.html) so railties would be reset and with that the executable shipped by railties would overwrite the wrong one.
Addition: Of course it only helps as long as the intrusive gem shipping a rails executable is also updated and no longer wants it to be installed.

Unable to run `rails g` or `rails c` without `sudo`

I am unable to run rails g or rails c, because of the following error message:
Could not find mysql2-0.3.18 in any of the sources
Run `bundle install` to install missing gems.
I can run 'rails server', which works fine, as expected. I also have ran bundle install, with no errors (this line is in my Gemfile: gem 'mysql2'). The database works fine with rails server, but seems to break with rails g/c.
Update:
I can run rails g and rails c with sudo. I think it's a problem with my path - I think I might have installed MySQL as root.
Have you tried running those commands using Bundler?
bundle exec rails c
bundle exec rails g

Why is "rails runner -e production" not working?

To solve my problem, which is discussed in "will_paginate error in production undefined method 'paginate'", I tried running:
script/rails runner -e production
and:
bundle exec script/rails runner -e production
on the server like suggested in this github issue: https://github.com/mislav/will_paginate/issues/308#issuecomment-17167158
But I get the following error:
bundler: command not found: script/rails
Install missing gem executables with `bundle install`
Bundle install doesn't help. Any suggestions?
I'm using: Ruby 2.0.0p247, Rails 4.0.0, Ubuntu 12.10 LTS, Unicorn, Capistrano
Try this instead:
bundle exec rails runner -e production.
Use bin/rails runner for rails4 (when the bin/rails file exists)

Resources