Viewing a Ruby on Rails script in my native browser - ruby-on-rails

I'm new to developing in Ruby and have mostly been using irb to experiment with code. For longer scripts, it would be helpful to be able to run them in my native browser (similar to how I run php scripts through MAMP). I believe there is a way to do this using localhost:3000 but I have not been able to get it to work. So my question is, what is the best way to view Ruby scripts in my native browser?

Well, running some Ruby code in IRB has nothing to do with using the Rails framework.
Follow a tutorial (for example this one) to learn the Rails framework itself now you have some understanding of the Ruby language.
Good luck.

I don't see how this would really be helpful to you, but it would be pretty easy to put your code into a simple Sinatra route and have that serve the result of the code into a browser if you want. Then you can just…
require 'rubygems'
require 'sinatra'
get '/' do
"Hello World! This is file #{$0} live from Boulder!"
end
And when you access the server on your local computer, it will print whatever you put in the method.
I would be interested to hear why you want to do this rather than IRB, though. This seems like kind of a perverse way to code in most cases.

Rack is a Ruby webserver interface, you probably want to use that to hook your ruby script up with a server.

Related

How do I run a ruby script from a webpage?

I'm a total newb when it comes to programming and I'm trying to connect to a rest API with ruby on rails since it seemed like a good option to pick.
Before I try to connect to the API i'm just trying to figure out how to run a script from a webpage but it just tries to save the .rb file when I navigate to it in a browser. The code i have in the file is:
puts "hello world"
I uploaded the .rb file to my web server and went to the URL and it just tries to save the file. Sorry for the stupid question but I can't figure out how to do it!
In general, to run off simple commands like that, you can run ruby scripts via the terminal:
ruby /path/to/ruby/script.rb
Otherwise, if you insist on using a webserver for such purpose (or, if your use-case requires so), you should look at simple ruby-based web servers/frameworks like rack, sinatra, etc.
Particularly, in rack, you can save the following file (lets, say as: hello.rb):
require 'rubygems'
require 'rack'
def application(env)
[200, {"Content-Type" => "text/html"}, "Hello world."]
end
Rack::Handler::Mongrel.run method(:application), :Port => 9292
Then, you can run the above file using terminal:
ruby /path/to/hello.rb
And, this will create a local web server with port 9292, and you can open the above page at: http://127.0.0.1:9292/, and the page will say: Hello world.
You need an app server in order to serve a web page. Look at sinatra, rails and rack.
http://m.onkey.org/ruby-on-rack-1-hello-rack
A classic web server (like Apache, nginx, Tomcat, etc.) do exactly what you are describing, as well as do many other functions. To run ruby scripts, you'll need a Ruby application server, and the web server needs to reverse-proxy requests to that server. Definitely take a some tutorials around Rails and you'll see tons of examples.
If you want to run a Ruby script on the client-side (instead of on a server), you can use a Ruby-to-JavaScript compiler such as Opal or ruby2js, or a Ruby-to-WebAssembly compiler such as run.rb.
For example, Opal translates puts "hello world" into
/* Generated by Opal 0.11.0 */
(function(Opal) {
var self = Opal.top, $nesting = [], nil = Opal.nil, $breaker = Opal.breaker, $slice = Opal.slice;
Opal.add_stubs(['$puts']);
return self.$puts("hello world")
})(Opal);

I'm a bit lost: how to render rails/ruby on a website?

Ok, long time php guy doing my best to pick up ruby on rails by developing a small website for myself using only ror instead of php.
However i'm a bit stuck, and i think it is because i'm not exactly understanding how rails actually works.
I am running centos 5.5 / apache2. I have successfully installed ruby, rubygems, and subsequently rails and passenger. All these are 'working', i can run ruby commands, gem commands etc.
But how do I start using rails on my website? If I create an .erb or .rb file with some simple ruby commands, it just displays as plain text when I navigate to it.
Do I need to configure apache to 'execute' .rb or .erb files (similar to the way .php files execute?).
Any help would be GREATLY appreciated!!
You might check out the Rails Getting Started Guide: http://guides.rubyonrails.org/getting_started.html
I suggest you read a huge tutorial on this topic: http://guides.rubyonrails.org/getting_started.html.
Ruby is the language, and Ruby on Rails is a framework. From the guide:
Rails is a web application development
framework written in the Ruby
language. It is designed to make
programming web applications easier by
making assumptions about what every
developer needs to get started. It
allows you to write less code while
accomplishing more than many other
languages and frameworks. Experienced
Rails developers also report that it
makes web application development more
fun.
I'd read Chapter 4, if you already know all this, as it talks about the actual webserver.
Rails comes with it's own webserver, so Apache isn't needed.
Sounds like you need to really get a handle on Rails and what it is first. This site has some great information for beginners, and should help you understand what you are working with and your next steps to get an application running:
http://guides.rubyonrails.org/getting_started.html
I've also heard some good things about the information here:
http://railsforzombies.org/
Check out Ruby on Rails Tutorial for getting started in Rails.
As #Blender mentioned, rails comes with an in build web server called webrick. (Think of it same as apache .. for now)
so all you have to do is go to the rails project directory, and run the command
if you are using rails < 3.x
ruby script/server
if you are using rails > 3.x
rails server
once u done this your rails project will start in webrick server and by default in port 3000
http://localhost:3000
you may consider apache/ some other rails server setup for production deployment, but to get started you dont need apache at all
and welcome to ruby world ! :D
HTH
sameera

What is the fastest test to determine if my webhoster supports ruby/rails

I am searching for a very quick and easy way to determine if my webhoster supports ruby / ruby on rails.
Is there an easy way like phpinfo() or something?
<?php
phpinfo();
?>
If you can run phpinfo on your server, you could check for "mod_passenger" in the list of installed Apache modules.
However, if your webhost doesn't clearly advertise its support for ruby/rails, stay away from it, it'll only give you headaches. There are cheap/free options to test your rails skills, such as Heroku.com.
Ruby is not like PHP. You can't just point your browser to a Ruby file to execute it. The best test is to SSH into the server (if possible) and try executing the ruby binary. Also, doesn't it appear on the host's feature list?
Presumably you can't ssh into your shared server? Otherwise, why don't you simply upload a ruby file and see if it works?

Ruby command line MVC framework?

I'm looking to write an app for the shell, *nix mostly. And I'm currently in love with Ruby, especially the 'rails way'.
So if there was a framework that applied rails like concepts to the commandline in ruby then that would be really fantastic.
I'v allready looked into SimpleCommand and Hirb, nothing quite what I was looking for.
To elaborate:
What I'm really looking for is a way to use a rails like (directory and application) structure to create a MVC command line application. So basically something like rails that doesn't respond to http, but instead reads and writes to the console.
Its not a shell, if i wanted that then irb works fine. It would be more like your options are A,B,C and they would work a bit like http links.
You should take a look at boson and hirb [2d] menus. First one for creation of commands, second one - for A,B,C options and custom/dynamic views for outputting data.
Maybe you should try script/console in your rails app. Is that what you wanted?

Are there any iPython-like shells for Ruby or Rails?

I love iPython and am learning RoR along with some libraries like Mechanize and I'd like to be able to easily see what I'm working with in terms of introspection. I would like to be able to type "." + TAB and see.
There is an irb tool to help autocomplete
require 'irb/completion'
You should take a look at Pry (an IRB alternative and runtime developer console). It isn't as advanced as the current version of IPython, but it's the most advanced developer console we have in Ruby.
try IRB http://en.wikipedia.org/wiki/Interactive_Ruby_Shell, or the rails console http://guides.rubyonrails.org/command_line.html. I don't know if either of them have autocomplete, but they are quality tools.
There is an IPython notebook backed by Ruby kernel. Details about the project
Wirble is a convenient, if infrequently-updated way of bringing together some of these tools. It's not quite as complete as iPython, but gets pretty close. Among other things, it pulls in irb/completion.
For rails, you can do the same, but use script/console from your rails' root directory to start irb.

Resources