Ruby on Rails is a WebDev framework written in the Ruby programming language.
You must be a MetaARPA member to use Rails (see memberships page).
Most Rails applications use a database, so you'll probably want dba membership as well (see memberships page). However MetaARPA members may use sqlite if they so desire.
Step 1: Run
mkhomepg if you have not already done so
tutorial for more information on the
Step 2: Use the rails command to construct the base for your new rails application:
It has become SDF policy to have the project that you want to be served under '$HOME/html/rails' in actuality you can locate the rails project directory anywhere and call it what you like. The 'railsctl' and 'ror' command expect the project that you want to host to be located in the afore-mentioned directory. So, if you want your server to be started when the host starts and 'railsctl' to find your project then you should symlink your current project to that location, or use that location.step 3:
Start the webrick server on the port that is equal to your uid, which you can obtain by executing 'id -u':
$ railsctl start - or - $ ruby $HOME/html/rails/my_first_project/script/server -p `id -u` => Booting WEBrick... => Rails application started on http://0.0.0.0:60844 => Ctrl-C to shutdown server; call with --help for options
Then point your browser at
http://yourdomain:<YOUR UID> You should now be looking at the default 'Welcome to Rails page'. Now you can begin to develop your rails app.
Rails is a Model-View-Controller framework. Rails accepts incoming requests from a browser, decodes the request to a controller, and calls an action method in that controller. The controller then invokes a particular view to display the results to the user. The good news is that Rails takes care of most of the internal plumbing that links all these actions. To write our simple Hello, World! application, we need code for a controller and a view. We don't need code for a model, because we're not dealing with any data. Let's start with the controller.
In the same way that we used the rails command to create a new Rails application, we can also use a generator script to create a new controller for our project. This command is called generate, and it lives in the script subdirectory of the my_first_project project we created. So, to create a controller called Say, we make sure we're in the my_first_project directory and run the script, passing in the name of the controller we want to create.
$ ruby $HOME/html/rails/my_first_project/script/generate controller Say exists app/controllers/ exists app/helpers/ create app/views/say exists test/functional/ create app/controllers/say_controller.rb create test/functional/say_controller_test.rb create app/helpers/say_helper.rb
The script logs the files and directories it examines, noting when it adds new Ruby scripts or directories to your application. For now, we're interested in one of these scripts and (in a minute) the new directory. The source file we'll be looking at is the controller. You'll find it in the file app/controllers/say_controller.rb. Let's have a look at it.
class SayController < ApplicationController end
Pretty minimal, eh? SayController is an empty class that inherits from ApplicationController, so it automatically gets all the default controller behavior. Let's spice it up. We need to add some code to have our controller handle the incoming request. What does this code have to do? For now, it'll do nothing. We simply need an empty action method.
Let's add an action called hello to our say controller. Adding a hello action means creating a method called hello in the class SayController. But what should it do? For now, it doesn't have to do anything. Remember that a controller's job is to set up things so that the view knows what to display. In our first application, there's nothing to set up, so an empty action will work fine. Use your favorite editor to change the file say_controller.rb in the app/controllers directory, adding the hello method as shown.
class SayController < ApplicationController def hello end end
Now let's try calling it. Navigate to the URL
http://yourdomain:60844/say/hello in a
browser window. (Note that in the development environment we don't have any
application string at the front of the path - we route directly to the controller.)
You'll see something that looks like an error.
It might be annoying, but the error is perfectly reasonable (apart from the weird path). We created the controller class and the action method, but we haven't told Rails what to display. And that's where the views come in. Remember when we ran the script to create the new controller? The command added three files and a new directory to our application. That directory contains the template files for the controller's views. In our case, we created a controller named say, so the views will be in the directory app/views/say. To complete our Hello, World! application, let's create a template. By default, Rails looks for templates in a file with the same name as the action it's handling. In our case, that means we need to create a file called hello.rhtml in the directory app/views/say. (Why .rhtml? We'll explain in a minute.) For now, let's just put some basic HTML in there.
<html><head><title>Hello, Rails!</title></head> <body> <h1>Hello from Rails and SDF!</h1> </body> </html>
Save the file hello.rhtml, and refresh your browser window. You should see it display our friendly greeting. Notice that we didn't have to restart the application to see the update. During development, Rails automatically integrates changes into the running application as you save files.
idcommand to find your uid
$ id uid=60844(phm) gid=500(arpa) groups=500(arpa),600(MetaARPA)
Note: if you do not see metaARPA here you're not in the metaARPA group and Rails won't work for you.