Ruby webpage shows only source code - ruby

I am trying to use Ruby to set up a home page. I currently have this code:
#!/usr/bin/ruby -w
puts "Hello, Ruby!";
It is saved as testing.rb. I am able to host files onto my school's public server so I had it in my school's directory such as school.ca/myname/public_html/testing.rb.
This has worked for previous HTML, CSS, Perl, ASP, and PHP programs. Now I am trying to set up Ruby and I'm having trouble. All it displays on the URL is my source code from above.
I changed the file permissions to 644.

You need a Ruby aware web server. There is one solution that makes running Ruby almost as easy as running PHP: Phusion Passenger. It can be installed as an Apache or Nginx plugins, two very common web servers. However, in order to install these plugins you usually require root access to your server, which I assume you don't have.
An entirely different solution would be to use a cloud service provider such as Heroku. They offer free plans as long as you are ok with your app not running 24h/day. In order to use this service, you have to be familiar with git though, but then it is as easy as uploading your code through git to run your application.
As for building your first Ruby web application, you should check out Sinatra. A simple hello world application would look as follows:
require "sinatra"
get "/" do
"Hello World!"
end
Sinatra is a Ruby Gem. You can install those libraries from the command line using a tool called gem:
gem install sinatra

I changed the file permissions to 644.
~/ruby_programs$ chmod 644 cgi.cgi
~/ruby_programs$ ls -al cgi.cgi
-rw-r--r-- 1 7stud staff 102 Nov 13 15:50 cgi.cgi
File permissions are displayed as follows:
First character is - or d: - means file, d means directory
Then there are three sets of three characters indicating permissions for owner, group and other:
r = readable
w = writable
x = executable
644 produces the permissions:
rw-r--r--
which are equivalent to:
owner: rw- (read, write)
group: r-- (read only)
other: r-- (read only)
You don't have to know who is an owner, or who is part of a group, or who falls in the other category to recognize that no one has permission to execute the file. You need to do this:
~/ruby_programs$ chmod a+x cgi.cgi #=>all + x => give execute permissions to everyone
~/ruby_programs$ ls -al cgi.cgi
-rwxr-xr-x 1 7stud staff 102 Nov 13 15:50 cgi.cgi
Now the permissions are:
owner: rwx
group: r-x
other: r-x
which means that now anyone can execute the file.
All it displays on the URL is my source code from above.
If you haven't already done so, try giving your ruby file a .cgi extension. Then use this code:
#!/usr/bin/env ruby
puts "Content-type: text/html\n\n"
puts "<html><body>Hello, Ruby!</body></html>"
Are you sure ruby is installed on your school's server?

Ruby is not a language that browsers can really interpret on their own without help - you need to either set up a server and have it print out an HTML webpage yourself, or use a framework that does so for you(i.e. Sinatra, Rails).

Related

Nitrous.IO 'Preview' only showing default page

I cannot see the apps running using 'Preview'.
The Ruby environment with the Rails framework is installed (able to see folders and files generated), and see application being built in Nitrous.IO IDE.
My installation shows I have the following installed:
- ruby 2.1.1p76
- Rails 4.1.0
- git version 2.0.0
- SQLite3
Building a simple blog app, I use the scaffold generator to create the MVC components needed for posts and comments:
1. $ rails generate scaffold post title:string body:text
2. $ rails generate scaffold comment post_id:integer body:text
I create the post and comment database tables using:
1. $ rake db:migrate
2. $ rake routes
3. $ rails server
Using 'Preview' to see app running on WEBrick, only get default webpage which says:
- "Routes are set up in the config/routes.rb"
- "running in development mode and haven't set a root route yet."
config/routes.rb file has the following:
Rails.application.routes.draw.do
resources :comments
resources :posts
# lines 6 -> 59 are comments giving coding examples of routes
end
==>> What do I need to do to 'Preview' Ruby apps??
Based on your comment above, I believe what you need to do is remove the public/index.html file and/or go to the route(s) you have created and specify one of them as root.
Also see this SO question and it's answer(s): deploying to heroku -- can't get rid of the "welcome to rails" default page
After you run rake routes command, it creates all the REST calls for you i.e. GET/POST/PUT/DELETE and as you have already created the scaffold you just need to invoke URL with the paths as /comment or /post and you will be able to access your blog application.
Ex: http://yourhost.com:3000/comment or http://yourhost.com:3000/post.

'bundle show' results in different paths depending on user, but on same system and Gemfile.lock

I have multiple users using a CLI app on the same system. In order to use a unreleased patch, the Gemfile points to a specific commit on github of the grit gem. The app has a Gemfile.lock. All users have the same $GEM_HOME and $GEM_PATH locations set.
Now, for all but one user 'cd app; bundle show grit' shows a path like this '$GEM_HOME/bundler/gems/grit-35b71d599549' (which exists cuz I ran bundle install). But for the odd-ball, 'cd app; bundle show grit' shows a path like this '/nfs/home_dirs/odd-ball/.bundler/ruby/2.0.0/grit-35b71d599549' (which doesn't exist). A Bundler::GitError is raised for this user.
I have looked for $BUNDLE_* environment variables and for ~/.bundle* configuration. I've also verified he has permissions to the $GEM_HOME/bundler/gems.
What other reasons could account for this difference?
Thanks.
I dug in and found that it came down to file permissions for this individual user. Specifically, in Bundler.requires_sudo?:
def requires_sudo?
...
# if any directory is not writable, we need sudo
dirs = [path, bin_dir] | Dir[path.join('*')]
sudo_needed = dirs.find{|d| !File.writable?(d) }
...
end
This user was not in the group allowed to write to "#{path}/lib". And I'm assuming that when Bundler.requires_sudo? returns true, bundler will divert install_path to the users home dir.

Using Heroku for static site: Assets won't show

I've just loaded up a static app to Heroku using this tutorial and everything works quite well, except my images aren't showing up. When the same site is hosted on my own server as a plain static site (not through Heroku), all of the assets load up without a problem.
Currently, I have a Gemfile, Gemfile.lock, app.rb, config.ru and public (static site directory) in my repository that I'm loading to Heroku through git push heroku master to push to Heroku.
My images are in public/img and even the assets directly referenced from html aren't showing up. When I use firebug lite in Chrome to check the asset directory, it seems as though the image files are there, but they don't seem to have the image data (from what I could tell).
I do not have any further ruby/rails files. Should I have a production.rb file somewhere? Am I missing out on something?
Currently, my setup on Heroku is the free package. Will I need to upgrade to a paid package to see my assets (I only have 2MB of assets)? I've tried creating an "assets" directory inside the "public" directory and placing the img directory in there, but still no luck.
Here is my config.ru
use Rack::Static,
:urls => ["/img", "/js", "/css"],
:root => "public"
run lambda { |env|
[
200,
{
'Content-Type' => 'text/html',
'Cache-Control' => 'public, max-age=86400'
},
File.open('public/index.html', File::RDONLY)
]
}
To diagnose issues like this where you believe the file contents on your dyno don't match the ones in your source, use heroku run bash to login into a remote, on-off dyno. This will drop you into a bash shell where you can explore the file system as seen by your dyno (although the dyno your shell is attached to is not the one actively serving your requests it will have the same filesystem contents).
$ heroku run bash
Running `bash` attached to terminal... up, run.4065
~ $ ls
pubic Gemfile Gemfile.lock app.rb config.ru
~ $ cd public/img
~/public/img $ ls -l
total 40
-rw------- 1 u36831 36831 2743 2013-02-15 18:54 facebook-1652d049.png
-rw------- 1 u36831 36831 2291 2013-02-15 18:54 feed-e8d78a2f.png
From here you should be able to see:
If the image files even exist on the dyno
If their contents are what you expect (do the file sizes match what you see in your local env?)

Why would I get "Errno::ENOENT: No such file or directory" when viewing a Sinatra form?

I am trying to follow this tutorial:
http://net.tutsplus.com/tutorials/ruby/singing-with-sinatra/
Got stuck in "We’ll also make use of a “view file”, which allows us to split the markup for a view into a separate file. "
I have my basics.rb file running fine.
And My files are stored as follows:
Desktop/RubyForm/basics.rb
Desktop/RubyForm/view/form.erb
However, now when i go to http://localhost:9393/form , I am greeted with:
Errno::EIO at /form
Input/output error - <STDERR> file: lint.rb location: write line: 398
sinatra.error
Errno::ENOENT: No such file or directory -
/Users/HelenasMac/Desktop/views/form.erb
UPDATE! : Got the form to work right after running ruby basics.rb and going to http://localhost:4567/form .
However, after I run "shotgun basics.rb" , I have to go to
http://localhost:9393/form, and that's when the form doesn't show up.
What am I doing wrong? Disclaimer: mega beginner to ruby and using the terminal.
Thanks in advance!
If you cannot get shotgun to work then the new recommended way to reload Sinatra seems to be rerun.
To use it:
> gem install rerun
> cd /Users/HelenasMac/Desktop/RubyForm
> rerun ruby basics.rb
Explicity Set a Views Directory
Unless you're using inline template for your views with enable :inline_templates, you may need to explicitly define a template directory if the default values aren't working for you. The docs describe how to set your views directory as follows:
:views - view template directory
A string specifying the directory where view templates are located. By default, this is assumed to be a directory named “views” within the application’s root directory (see the :root setting). The best way to specify an alternative directory name within the root of the application is to use a deferred value that references the :root setting:
set :views, Proc.new { File.join(root, "templates") }
You may also need to explicitly set :root, and make sure that both :root and :views make sense from your current working directory.

Ruby separate large source files into multiple files

I am writing a Ruby script which was supposed to be a small thing but has grown quite large, way to large to have everything crammed into one source file. So I am trying to separate the project into different files. I have four classes and I want to put each in its own separate source file.
What I did:
I moved all of the classes into their own files so now I have this
proj/GoogleChart.rb
proj/BarChart.rb
proj/PieChart.rb
proj/GroupedBarChart.rb
Now that they are in other files I am getting uninitialized constant GoogleChart (NameError) in all of my subclasses on the line where I inherit from GoogleChart, i.e.
require 'GoogleChart'
BarChart < GoogleChart
Can anyone tell me what is wrong?
Thanks
EDIT
Using ruby version 1.8.4
Also I have tried using the absolute path:
require 'C:/Documents and Settings/proj/GoogleChart.rb' and this is still producing a NameError
In Ruby 1.8.x, the . is part of your load path. So you should at least try to debug that by including something like:
puts $:
require 'GoogleChart'
class BarChart < GoogleChart
end
and load that in an IRB session:
Open the session in your directory proj.
Enter there require 'BarChart'
Look at the result.
For me it is:
c:\apps\ruby\test\proj>irb
irb(main):001:0> require 'BarChart'
C:/Users/mliebelt/.pik/rubies/Ruby-187-p334/lib/ruby/site_ruby/1.8
C:/Users/mliebelt/.pik/rubies/Ruby-187-p334/lib/ruby/site_ruby/1.8/i386-msvcrt
C:/Users/mliebelt/.pik/rubies/Ruby-187-p334/lib/ruby/site_ruby
C:/Users/mliebelt/.pik/rubies/Ruby-187-p334/lib/ruby/vendor_ruby/1.8
C:/Users/mliebelt/.pik/rubies/Ruby-187-p334/lib/ruby/vendor_ruby/1.8/i386-msvcrt
C:/Users/mliebelt/.pik/rubies/Ruby-187-p334/lib/ruby/vendor_ruby
C:/Users/mliebelt/.pik/rubies/Ruby-187-p334/lib/ruby/1.8
C:/Users/mliebelt/.pik/rubies/Ruby-187-p334/lib/ruby/1.8/i386-mingw32
.
=> true
So the require is successful for me, and the . is part of the path (as it should). As you can see, I am working with Ruby 1.8.7, I don't know if anything has changed since 1.8.4 that is relevant here.
So please describe exactly how you run your file:
Have you opened a shell to run the file?
What is the current working directory of that shell?
Do you run by double-clicking it?
It only works when you are in your proj directory and run there (with ruby in your shell path) ruby BarChart.rb.

Resources