Why doesn't “rails s” work from the app directory?

前端 未结 7 2047
醉酒成梦
醉酒成梦 2020-12-09 04:33

I\'m in my app folder, but the command rails s is not working. I read through quite a few posts on Stack Overflow, and most of them seem to be from users who ar

相关标签:
7条回答
  • 2020-12-09 04:56

    If you use rvm or rbenv for instance to keep multiple ruby versions, maybe your default rails version for that specific ruby version is different than the project you are trying to run and therefore it's not being able to detect your application.

    To make sure you are using the right rails version you can compare both results. This is what I've got:

    $ rails -v
    Rails 3.1.0
    

    to

    $ bundle exec rails -v
    Rails 5.0.0.1
    

    In this case, you can keep the default rails version and then use:

    $ bundle exec rails server
    

    Or install the specific rails gem to that very ruby version with:

    $ gem install rails -v 5.0.0.1
    $ rails -v
    Rails 5.0.0.1
    

    And then get it working with the less verbose command:

    $ rails s
    

    I hope this becomes helpful to other folks in the same situation!

    0 讨论(0)
  • 2020-12-09 04:57

    First check with your location path and then

    bundle install
    

    If still does not work, enter

    /bin/bash --login
    bundle install
    
    0 讨论(0)
  • 2020-12-09 05:01

    You likely have not bundled your gems yet:

    # from command line
    bundle install
    
    0 讨论(0)
  • 2020-12-09 05:06

    It seems to think you are not in a rails directory (your output is saying the only valid way to use rails is with rails new).

    Depending on your version, Rails identifies this differently. On 3.2, it checks for a file at script/rails. Now that 4.0 has been released, it looks for either script/rails or bin/rails (https://github.com/rails/rails/blob/207fa5c11ddf1cfd696f0eeb07d6466aae9d451e/railties/lib/rails/app_rails_loader.rb#L6)

    Presumably you can get around this by creating the file rails in your script directory (if you do not have a script directory, create one in the root of your app):

    #!/usr/bin/env ruby
    # This command will automatically be run when you run "rails" with Rails 3 gems installed from the root of your application.
    
    APP_PATH = File.expand_path('../../config/application',  __FILE__)
    require File.expand_path('../../config/boot',  __FILE__)
    require 'rails/commands'
    

    Of course, it's worth wondering why you don't have this file in the first place. Might be worth making sure your rails is the version you want to be using first (rails -v if the version is newer, this post will show you how to create the new app using the older version).

    0 讨论(0)
  • 2020-12-09 05:08

    All the above answers didn't help me. What solved my problem for Rails 4 was to run command in the root directory of my application:

    rake rails:update:bin
    

    After that running rails s was running as expected.

    0 讨论(0)
  • 2020-12-09 05:14

    I had this problem, took me a few minutes to realize I'd forgotten to change active Ruby version with chruby. Different Ruby implied a different Rails version, which looked for the relevant file in another folder.

    0 讨论(0)
提交回复
热议问题