| home | resources | search | news | join | members: 6961 |
Hiveminds | Sun, 2007-09-02 18:34 tags: Tutorials, Web Developer, eRuby Ruby is the interpreted scripting language for quick and easy object-oriented programming. It has many features to process text files and to do system management tasks as languages like Perl or Python do. Ruby is simple, straight-forward, extensible, and cross-platform portable language that has a syntax that is easy to understand. One of the strengths of Ruby is that it strives to be powerful but not complicated. Ruby has advantage over other OOP languages in that it can be used to code procedurally. Here we show how to setup a windows development PC and a shared web host so you can start developing Ruby web applications in the same way you do PHP, embedded in HTML. Embedded Ruby, eRuby, is easy to learn and has features that make it more useful than other web scripting languages. Now you can use eRuby to create robust web applications and websites. This article will have you doing Ruby on the web in minutes. Using Ruby on the webWhat is eRuby?eRuby is an extension to Ruby that allows you to use the power of Ruby and embed its code into HTML files. The way eRuby does this is by inversing the HTML code into print statements before sending the code on to be interpreted. Code in the HTML file is translated into a mix of single and multi-line commands. This is all sent to the Ruby CGI interpreter for processing. Why not Ruby on Rails?I could not very well write this without mentioning Ruby on Rails. Rails is probably the main reason that Ruby is becoming popular outside of Japan. But Rails is not the only way to use Ruby on the web. Rails also has many drawbacks in that it removes almost all of the ease of deployment of Ruby. Compared to the simple FTP upload of eRuby, deploying a Rails application is complicated, time consuming task and just not something that you will want to do on a repetative basis. Rails also is a lot of work if you just want to do a single webpage application that contains everything. Web hosting companies are also a large factor in using Rails. To do Rails properly you need to have ssh knowledge and be prepared to do some command line manipulation of files. The needs of Rails makes a shared hosting environment difficult to set up and administrate. One of the most troublesome of the Rails drawbacks for me and the reason that I decided to go with eRuby is the that Rails was taking up all my time with Rails problems and troubleshooting. I was learning a lot about using Rails but not much of the Ruby programming language. Someone suggested we go with CGI instead and while researching this we found eRuby. eRuby is a much easier way of using, learning and getting the power of Ruby on to a website. With eRuby deployment is a easy as uploading your files to your web server. This is to say deploying an application or webpage is no harder in eRuby than it's counterparts like PHP or ASP. I honestly believe that if Ruby popularity is going to increase that eRuby will be the reason. Unlike Rails eRuby has a very shallow learning curve and does not require a lot of effort by web hosting companies to set up. About this tutorialIn this tutorial I am going to assume a few things that require that you be familiar with the technology used. Although nothing in this writing is "geeks only" difficult it does require that you be familiar with the terminology or take the time to read a few paragraphs here and there from other resources. This tutorial is also geared towards Windows users and those wanting to do web development on Windows PCs. This tutorial is not intended to be a lesson on the Ruby language but on how to get started by setting up both a working development and production environment. Once you get your set up going you can go on to other articles covering how to do some useful application in Ruby Setting up a development PCWampserver5Is a free and opensource Apache webserver package for windows. The nice thing about wampserver is that it is a lightweight package and has a exceptionally friendly user interface. There are of course many other flavors of windows Apache packages. You are welcome to use one of those as long as you are confident that you can carry out the configurations needed to setup eRuby. Click on the executable and follow the instructions. Ruby One-click installerThis is a single file that will install ruby on to your Windows computer. The only thing you have to do is click once or twice and wait. It contains the Ruby language itself, dozens of popular extensions and packages, a syntax-highlighting editor and execution environment, and a Windows help file that contains the full text of the book, Programming Ruby: The Pragmatic Programmer's Guide. Click on the executable and follow the instructions. Ruby is also set into the your Windows path so that you can run it from any directory. eruby.exe( for set up on the localhost)To interpret HTML pages that have Ruby code embedded in them you need a windows binary of the eRuby interpreter. You can either compile this binary yourself using minGW or you can use the pre-compiled file dowloadable from one of the minGW mirrors. Once you have downloaded the binary add the eruby.exe to the bin directory where you installed Ruby. Apache 2 configurationThis tutorial is not geared towards the raw beginner so I will go straight to the instruction set for configuring the Apache web server. If you have never done this before then you may want to read up on it and train a bit before going further. 1. in httpd.conf add:
AddType application/x-httpd-eruby .rhtml2. add "+ExecCGI" directive to pertinent directories sections or add it in the global DocumentRoot(be warned thought, this allows executable cgis in ALL DocumentRoot an subdirs good for localhost but probably not smart for real servers). 3. You will also want to add in this directive which tells Apache where to look to get the file association. Normally a shebang is called for in the eRuby file. But since we want to get easy deployment getting rid of the shebang is necessary. Otherwise you will have change or remove the shebang before uploading your files to the shared host. Futher along in this tutorial you will see why this directive is needed but to save having to go back into http.conf and restarting Apache again we are going to do this now.
ScriptInterpreterSource "registry"
4. Besure to update the Directory index file list to reflect *.rhtml.
5. add as first line to the cgi files(*.rb,*.rhtml...)
#!f:/ruby/bin/eruby.exe ( this should be the path to Ruby on your PC )
Save this in docroot as eruby_test.rhtml, run it.
The CGI shebangIf you'll notice the test file code has a shebang path at the top of it. The shebang directive is used to control how Apache finds the interpreter used to run CGI scripts. Since eRuby is running as a CGI this is necessary. But when you are going to upload your *.rhtml files to your production webserver a shebang is not needed in most cases. This is going to depend on the servers configuration but I think in most scenarios you will be developing on Windows and the uploading the finished scripts to a Linux OS based web host. When you deploy to this server a shebang is not needed but for the purposes of testing we will leave this in place and then work on removing it later. The Apache web server uses the interpreter pointed to by the shebang line (first line, starting with #!) in the script. On Win32 systems this line usually looks like:
#!C:/ruby/bin/eruby.exe
or, if eruby is in the path, simply:
#!eruby
If changing the path of all of your development PC files before uploading to your shared hosting account is trivial then you might not want to concern yourself with the next section. But if you are planning on distributing your code then you will want to make it as user friendly and generic as possible. Below are the instructions for running CGI on Windows without using a shebang line in your files. Configuring Windows and Apache 2This next step is an advanced windows user procedure which is why I saved it for last. After you have tested the environment and had a little fun with some simple Ruby you will want to setup for deplyment to a shared host. Nothing special is needed for this but a windows configuration change is called for to allow your *.rhtml to run on the shared host unmodified. You want the script to run without using a shebang at the top. The previous http.conf setting for the directive
ScriptInterpreterSource "registry"
will cause the Windows Registry tree HKEY_CLASSES_ROOT to be searched using the script file extension (e.g., .rhtml, .rb, .rbx) as a search key. The command defined by the registry subkey Shell\ExecCGI\Command or, if it does not exist, by the subkey Shell\Open\Command is used to open the script file. If the registry keys cannot be found, Apache falls back to the behavior of the Script option. In order for ScriptInterpreterSource Apache directive to work, you also need to add an entry to the Windows Registry so Apache will use Ruby to execute *.rhtml files. Go to start->run and in the input box write "regedit" to start the windows registry editor. Create a key HKEY_CLASSES_ROOT\.rhtml\Shell\ExecCGI\Command with the default value of the full path of eruby.exe. For example C:\ruby\bin\eruby.exe. ![]() You can also create a registry execution file. Just copy the follwing to a notepad file and save it with the *.reg extension ex. "eruby_rhtml.reg". Clicking on the file will update the registry and give a popup about its install.
Windows Registry Editor Version 5.00After doing the above procedure remove the shebang line from your test file and run the script in the browser. If everything is okay then you will get the same output. If Apache cannot find a file association in the registry then it will throw an internal 500 error. SecurityBe careful when using ScriptInterpreterSource "registry" with ScriptAlias'ed directories, because Apache will try to execute every file within this directory. The Registry setting may cause undesired program calls on files which are typically not executed. For example, the default open command on .htm files on most Windows systems will execute Microsoft Internet Explorer, so any HTTP request for an .htm file existing within the script directory would start the browser in the background on the server. This is a good way to crash your system within a minute or so. The option Registry-Strict which is new in Apache 2.0 does the same thing as Registry but uses only the subkey Shell\ExecCGI\Command. The ExecCGI key is not a common one. It must be configured manually in the windows registry and hence prevents accidental program calls on your system. Setting up the shared host.htaccess support on your web hostTo gain access to the CGI executable on a shared web host you should use .htaccess. It is a simple thing and it allows you to control where eRuby can be used on the directory system.
eruby.cgi ( for set up on the web host)If your web host has eruby installed and you have access to ssh then you will only have to copy eruby from the server to your accounts root folder. When you copy the files from the folder it will auto-matically be renamed eruby.cgi
You can also compile your own eRuby and install it if your web host allows this on their servers. In most cases copying the web hosts file is best because you know that it is working and safe. But you may be in a situation where the web host has not installed eRuby but has nothing against you doing it yourself.
ConclusionNow that you have both a development computer and a web host environment that supports eRuby you can start taking advantage of the Ruby language. Let's go through a simple web application just to get a taste of what Ruby provides. A demo can be found here.
ResourcesWampserver http://www.wampserver.com/en/index.php Ruby one-click installer http://rubyforge.org/frs/?group_id=167 eRuby eruby.cgi
This article brought to you by the
Hiveminds Magazine - Staff. Contact us if you want to post an article or announcement anonymously Thoughtbox - So what did you think? |
Windows London Hotels
Adobe Flex Content Management Systems Laptop Reviews
Web Developers Just Dial International Calls
Content Management Systems Excel Training Courses
Drupal excel courses in london
PHP Data Recovery
|
||||||||
NewsletterGet updates on Hiveminds services, articles and downloads by signing up for the newsletter. |
Editor's choiceSome of the better articles, stories and tutorials found at Hiveminds. |
Find moreFind more of Hiveminds articles, stories, tutorials and user comments by searching. |
Picked linksHand picked websites and articles from around the web that provide quality reading. |