Emulate terminal to run ruby code on browser - ruby-on-rails

I want to create a unix terminal on my RoR website. On this terminal, the user should be able to execute ruby code. I have no idea how can I create such terminal on browser.
Can anyone guide me about required resources, available ruby gems and technology to be used?
Thanks a lot.

you can use backticks to run shell command
#resut= `#{cmd}`
or eval for ruby command
#resut= eval cmd
note: those are very dangerous to use in real world app

Related

Best way to use rails console with cloud foundry

We've been experimenting with CF over Heroku and running into some issues. One of them deals with accessing the rails console in a CF AI. We're using Pivotal's PWS and have tried a number of things, including:
cd app; export HOME=$(pwd); source .profile.d/0_ruby.sh; rails c
and
cd app; export HOME=$(pwd); source .profile.d/*.sh; rails c
Both of which are hit or miss and typically don't work.
It seems a bit ridiculous that it's THIS much work to access the rails console via CF. I feel like there has to be a better, faster way.
Does anyone have any tips?
For anyone saying we should cf ssh in, here is what happens:
vcap#2f4663e4-f876-490c-65e2-a498:~$ cd app
vcap#2f4663e4-f876-490c-65e2-a498:~/app$ ls .profile.d/000_multi-supply.sh 0_ruby.sh
vcap#2f4663e4-f876-490c-65e2-a498:~/app$ source .profile.d/0_ruby.sh
vcap#2f4663e4-f876-490c-65e2-a498:~/app$ cd ..
vcap#2f4663e4-f876-490c-65e2-a498:~$ rails c
bash: rails: command not found
vcap#2f4663e4-f876-490c-65e2-a498:~$ source app/.profile.d/000_multisupply.sh
vcap#2f4663e4-f876-490c-65e2-a498:~$ rails c
bash: rails: command not found
As of writing this, to fire up a Rails console run cf ssh my-app -t -c "/tmp/lifecycle/launcher /home/vcap/app 'rails c' ''".
This will SSH into the container and use the lifecycle launcher, which sets up the environment for you, to execute the command.

Why my rails command works differently with system() than in terminal?

The command that I'm trying to run is rails _3.2.13_ new App
When I run it in command line it creates a Rails app with version 3.2.13, but when I run it with system "rails _3.2.13_ new #{self.name} -T -B" it creates a Rails app with the latest version of Rails not 3.2.13 version.
This is a result of Ruby using /bin/sh to execute shell commands, whereas you are probably using /bin/bash in Terminal on a daily basis. The way each is loaded and the specific configurations present in each will alter the configuration.
If you run which rails from both calls to system and in your terminal you'll likely see different paths. Check echo $PATH and you'll likely see different results too.
To resolve the situation, you can check out What's the difference between .bashrc, .bash_profile, and .environment? which will give you a much better understanding of what's going on, then adjust your shell configuration to accomodate.

New to Command Prompt - Do I need to prefix every command with "Jruby -S ..."

I'm new to using Window's Command Prompt, and also to developing with Ruby on Rails. Possibly a silly question but one that I'm sure everyone who learns with CodeCademy will end up asking; right now I'm prefixing every command for my project with 'Jruby -S ...", for example:
C:\users\MyName\MyProject> Jruby -S rails new MyApp
...
C:\users\MyName\MyProject> Jruby -S bundle install
...
C:\users\MyName\MyProject> Jruby -S rake db:migrate
Can I use some kind of alternative shell to save me typing Jruby -S every time? I'm aware of bash and powershell but have basically zero knowledge of whether I should be using them...
Thanks folks!
EDIT
Lots of helpful suggestions below, but I was really looking for a shell to mimic the functionality of the console on codecademy.com (which I believe is supposed to work like a Mac's 'bash' program?). Thanks anyway.
I'm new to using Window's Command Prompt
The CMD works very similarly to the GUI/Shell -- you have to call applications and then run commands with them.
The difference between CMD and windows is that CMD is "naked" - you have to ensure all the paths are correct, and that you're calling the correct application each time.
For example, calling rails server literally translates as:
Program = ruby.exe / rails
Command = server
CMD uses the PATH environment variable to make this process smoother.
The PATH var basically allows you to reference applications on your computer from the CLI (command line interface). This means that if you have an application (EG ruby.exe), you can add the ruby.exe directory to your PATH variable, allowing you to call ruby ... straight from cmd.
--
In your case, I don't have much experience with JRuby; I do know, however, that if you want to invoke the functionality of that application, you have to call it from the cli.
Hopefully my answer gives some context.
You can do that with powershell.
I'm sure that there should be a better way to do that, but you can try this
$ruby = "Jruby"
$s = "-S"
& $ruby $s rails new MyApp
I don't work on windows, however the jruby zip files on the download site have a bin directory with .bat and .exe files for jruby, rake, and gem. You could just add the directory you installed jruby to and the 'bin' subdirectory to your PATH to start.
set JRUBY_HOME= your_installed_jruby
set PATH= %PATH%;%JRUBY_HOME%\bin
http://jruby.org/download
I don't know what the windows installer does, but I would think it would do something similar.

How do I run 'rails server' inside a shell script?

I'm trying to write a shell script that automatically runs my rails app in a virtual machine.
My script code is this:
#!/bin/sh
PATH='/usr/local/rvm/gems/ruby-2.0.0-p481/bin'
cd /home/lgdelacruz/SampleApp
rails server
But for some reason it doesn't see all the dependencies. this gives me the error
/usr/bin/env: ruby: No such file or directory
I'm positive ruby is installed in the virtual machine. I can run rails server by manually going inside my virtual machine going to my SampleApp folder and running rails server there and everything works fine. But for some reason when I put all that in a shell script. it doesn't work.
You've probably got to initialize RVM in your script first. Try putting this line in:
source "$HOME/.rvm/scripts/rvm"
You might also need to specify a gemset, if you're using something other than the default:
rvm use #mygemset
See the RVM scripting docs for details.
In your shell script, you've reset your path to only include /usr/local/rvm/gems/ruby-2.0.0-p481/bin. ruby is usually installed somewhere like /usr/local/bin
instead you could concatenate that directory onto the end of your existing path.
something like:
export PATH=$PATH:/usr/local/rvm/gems/ruby-2.0.0.p482/bin

rails s command does not run from ssh

I am using Putty to connect to my localhost, and I don't have any problems apparently, however, when I run command rails s to start my rails 4.0.0 application from Putty, it gives me this message:
jose#jose-laptop:~/rails/dedicated-agenda$ rails s
The program 'rails' can be found in the following packages:
ruby-railties-3.2
ruby-railties-4.0
Try: sudo apt-get install
I don't get that message from the terminal though, the application starts running just fine.
I had to reinstall ubuntu so I upgraded to ubuntu 14.04 just in case you need to know.
I don't know if I am missing something in my ssh settings or how could I use rails s from Putty.
Thanks in advance.
Your PATH environment variable is set differently when you are executing programs in an interactive shell and by ssh(using putty).
Use absolute path of the program to not depend on the PATH variable.
You can also set the right PATH variable at ~/.profile file and load the updated variables using the command source ~/.profile.
Now, you should be able to run the command.
You can use the command
>which rails
to see where rails is installed on your working session.
Then you need to make sure that is in your path when you ssh in.
If you are ssh'ing in as a different use then that user may not have permission to see the rails executable.

Resources