rails application command - ruby

According to output from "rails" command, there is "rails application" command. When I use it I see the output from "rails new" command.
...
Description:
The 'rails new' command creates a new Rails application with a default
directory structure and configuration at the path you specify.
You can specify extra command-line arguments to be used every time
'rails new' runs in the .railsrc configuration file in your home directory.
Note that the arguments specified in the .railsrc file don't affect the
defaults values shown above in this help message.
Example:
rails new ~/Code/Ruby/weblog
This generates a skeletal Rails installation in ~/Code/Ruby/weblog.
See the README in the newly created application to get going.
Mb I`m doing something wrong? Or these commands do the same?
Sorry for my bad writing.

rails application is not a valid command.
The rails command line tool will print out the man page if an invalid command is given. Most command line tools do the same.
Run rails nonexistent_command and it will print out the man page.
If outside of a rails app, it will print the man page for rails new. If inside a rails app dir, it will print out the other possible commands. e.g rails generate, rails console, etc.

Related

How to run app via Ruby shell command, when same command works on command line

I have a simple Ruby (Sinatra) server that starts up without issue from the command line with ruby app.rb. But when I execute the same command via my command line app, either with `ruby app.rb` or with system("ruby app.rb"), I get this error:
app.rb:1:in `require': cannot load such file -- sinatra (LoadError)
from app.rb:1:in `<main>'
The opening line of app.rb is simply require 'sinatra'. The Sinatra gem is installed in my system, of course; I don't understand why the interpreter is acting as if it's not there.
While troubleshooting, I decided to add Sinatra to the Gemfile of the command line app that is calling app.rb. Lo and behold, now that the parent process has access to Sinatra, now it works (i.e., system(app.rb) successfully starts the Sinatra server). But when I exit the command line app, a Sinatra server is always there, saying:
[2018-12-18 23:17:37] INFO WEBrick 1.3.1
[2018-12-18 23:17:37] INFO ruby 2.4.0 (2016-12-24) [x86_64-linux]
== Sinatra (v2.0.4) has taken the stage on 4567 for development with backup from WEBrick
[2018-12-18 23:17:37] INFO WEBrick::HTTPServer#start: pid=27384 port=4567
So I have to Ctrl-c to exit the command line app.
Question: Is there a way to spawn an independent Sinatra process/server, as I was trying to do with system("ruby app.rb"), without installing it in the parent app (the command line app)? I also tried using Process.fork followed by Process.wait, but that didn't help.
Self-answered, since I found a solution, and nobody else has answered it:
Instead of executing just ruby app.rb, which by itself runs in the same environment as the surrounding program and hence uses the same Gemfile (ignoring the one referred to in the spawned script), execute:
system("BUNDLE_GEMFILE='./Gemfile' && ruby app.rb")
That's all there was to it! This tells Ruby to load the correct gemfile when it runs the program.
Personal note: Many thanks to all of you in the discussion, above, on the question, as well as the local Ruby group, my best friend, and my 12-year-old coder son (who actually supplied the exact code). Now I just have to figure out how to stop the spawned process without stopping the surrounding program. Can't just use "Ctrl-C." But that's a different problem!

rails console Cloud9 - Can't open console

NOTE - I solved my own problem. The issue was I was running the "rails console" command from the workspace, instead of of the sample_app directory.
When I try to run "rails console" in Cloud9, I get the below. All I'm trying to do is open the console - what am I missing? Thanks!
~/workspace $ rails console
Usage:
rails new APP_PATH [options]
Runtime options:
...
Rails options:
...
Description:
The 'rails new' command creates a new Rails application with a default
directory structure and configuration at the path you specify.
You can specify extra command-line arguments to be used every time
'rails new' runs in the .railsrc configuration file in your home directory.
Note that the arguments specified in the .railsrc file don't affect the
defaults values shown above in this help message.
Example:
rails new ~/Code/Ruby/weblog
This generates a skeletal Rails installation in ~/Code/Ruby/weblog.
See the README in the newly created application to get going.
You're probably in the workspace directory. You need to be in your app's directory:
cd app_name/
or
cd ~/workspace/app_name/

how to use RVM to a ruby app engine?

i wanna make a simple ruby App Engine in rails just like heroku, i'm dealing with a problem now.
My idea was:
1.use rails to establish the App Engine, use a class 'App' to handle all apps.
2.when a user create an ruby app he should offer it's git path
3.when the user deploys it, my app engine will do these things:
clone the git to a path in my server (done
use RVM to designatine the ruby version witch user wanted and make a gemdir for the project (some problems here
create a nginx conf for the project, then include it and reload nginx (i can do it
Problems in the second step:
codes here:
def start_thin
Dir.chdir(proj_path) do
system('rvm use ruby-1.8.7-p352#testname --create')
system('gem env gemdir')
success = system ('thin start -s3 --socket ' + self.proj_sock)
if success
return true
end
end
return false
end
when the code runs here, the log told me "RVM is not a function...blahblah", i know something about the login-shell and non-login-shell, then i try to fix it via editing .bashrc but same problem occurred.
And if i ignore it, the app can't be deployed, because of a Load Error :
myapp.rb:2:in `require': cannot load such file -- sinatra (LoadError)
if i open a terminal in that app directory, i can use thin to start it.
i wanna know how to run cmd just like in a terminal, without all these odd problem?
or how to edit my method to fix it?
Thanks!
Thanks Casper and GhostRider.
The user and rvm settings are correct.
After lots of googles and tests i found it's impossible...
Finally I fixed it by using RVM's ruby api instead of running system command.
Such as :
require 'rvm'
env = RVM.current
env.gemset.create('app1')

Rails -- is IRB necessary?

I am following Michael Hartl's RoR toturial and there are multiple places where he uses IRB, often to add users to the database. When I use rails console to open IRB and then create a User in the database everything works fine, but if I try to do the same thing by running the same line of code from a file like test.rb in the directory of my application it doesn't work b/c it says it can't find the User model. Is there any way I can run these lines of code (i.e. for putting a user into a database) from a .rb file rather than from the IRB?
For a separate script look into rails runner. It loads the Rails backend so you have access to all the models and exists for this purpose.
From the "Ruby on Rails Guides":
runner runs Ruby code in the context of Rails non-interactively. For instance:
$ rails runner "Model.long_running_method"
If you're just using test.rb as a convenience to save and re-run console commands, you could do this:
rails console < test.rb
Or, as a bit of a hack, put this at the top of your test.rb:
require 'config/environment'
And invoke it from the app's root directory like this:
ruby -I . test.rb
Placing a ruby file in the folder of your app doesn't automatically load up your Rails app. You need to explicitly load the config/environment.rb file to load the Rails app.
If your test.rb is in the root of your app, you can do something like
require File.expand_path("../config/environment", __FILE__)
# Access your models here

Need help with starting RoR: Command "rails server" does not result in "Booting WEBrick"?

I need help to get started with RoR.
I currently follow this guideline:
http://allaboutruby.wordpress.com/2009/07/20/installing-rails-on-windows-3-years-later/#comment-11099
I followed step 1 through 3 w/o problems.
In step 5: I can get the webserver through WEBrick working.
When i put
"rails server"
instead of getting "Booting Webrick", i get "rails new_path option"
thus when i try 127.0.0.1:3000 in the browser... it does work.
Can anyone guide me on this on how to get it up and runnning? (Im a total newb for now...so i need specific explanations! thanks!)
In your tutorial i can't see the command 'bundle install' - it's checking and installing all necessary gems in your system. So why you don't use another great rails tutorial - http://ruby.railstutorial.org/ruby-on-rails-tutorial-book
I'm guessing you are running windows, on which rails can be a little awkward. You'll probably need to run the rails server command by pointing ruby at the server script. On windows, your rails "commmand" is actually just a .bat file that lives in the /bin file of your ruby installation, and that .bat file just passes the arguments to ruby. If you look at the rails gem that is installed on your machine, you'll see the files that correspond to the normal first argument of a rails command (console, generate, server, etc). You might find it helpful to copy these to the /script directory of your application, and when you want to run a rails command you can just run "ruby script\server" from your application's main directory, though there may be more accepted ways of getting the same result.

Resources