Rolling On Rails With RubyStack

Rolling On Rails With RubyStack


This article is intended for all those people that want to start developing Ruby on Rails applications with RubyStack. Now that InstantRails project is no longer maintained, RubyStack is the simplest way to start developing Rails applications. So we decided to write this article to guide you through all the needed steps to develop a web application just in minutes. It takes some parts from the already existent tutorial for InstantRails Rolling with Ruby on [Instant]Rails. We hope you enjoy and do not hesitate to give us your feedback in our Forums.



What is Ruby?


Ruby is a pure object-oriented programming language with a super-clean syntax that makes programming elegant and fun. Ruby successfully combines Smalltalk's conceptual elegance, Python's ease of use and learning, and Perl's pragmatism. Ruby originated in Japan in the early 1990s. It has become popular worldwide in the past few years as more English-language books and documentation have become available (and its popularity has really taken off since the introduction of Rails!).



What is Rails?


Rails is an open source Ruby framework for developing web-based, database-driven applications. What's special about that? There are dozens of frameworks out there, and most of them have been around much longer than Rails. Why should you care about yet another framework?

What would you think if I told you that you can develop a web application at least ten times faster with Rails than you can with a typical Java framework? You can, withoutmaking any sacrifices in the quality of your application! How is this possible?

Part of the answer lies in the Ruby programming language. Rails takes full advantage of Ruby. The rest of the answer is in two of Rails' guiding principles: less software andconvention over configuration.

Less software means you write fewer lines of code to implement your application. Keeping your code small means faster development and fewer bugs, which makes your code easier to understand, maintain, and enhance. Very shortly, you will see how Rails cuts your code burden.

Convention over configuration means an end to verbose XML configuration files--there aren't any in Rails! Instead of configuration files, a Rails application uses a few simple programming conventions that allow it to figure out everything through reflection and discovery. Your application code and your running database already contain everything that Rails needs to know!



What is RubyStack?


For developing a complete Rails web application you need to have properly installed and configured at least Ruby interpreter, the RubyGems package manager for Ruby, the Rails gem and a MySQL server. That sounds complicated... Well, it is. But don't worry because RubyStack will do all this work for you and will install some bonus additions too.

The BitNami RubyStack is an installer that greatly simplifies the installation of Ruby on Rails and its runtime dependencies. It includes ready-to-run versions of Ruby, Rails, MySQL and Subversion. RubyStack is distributed for free under the Apache 2.0 license.



Installing and Configuring Rails: RubyStack


Point your browser to  RubyStack and download a copy of the RubyStack. Double click the installer and follow the on-screen instructions.For detailed instructions about how to install RubyStack check out  Installing RubyStack Tutorial.

 



Generating our Rails Application


Now that we have RubyStack installed, the next step when developing a Rails application is to create an empty one with the command "rails" and configuring it by writing its "config/database.yml" configuration file. Once again RubyStack has done all this work for as and has created and configured a new rails application with default location at "C:/Program Files/BitNami RubyStack/projects/rubystack". For detailed instructions about how to install and configure a new application you can go to our tutorial From InstantRails to RubyStack.



Starting the Web Server


We are going to do the most of the work from an "Use Ruby" command line. If you go to "Start -> All Programs -> BitNami RubyStack -> Use Ruby" you will see a command line window like this:rubystack-userubyFrom there you can run commands like "ruby", "mysql", "svn", etc. Let's move to our application's directory and launch the web server:

cd projects/rubystack

ruby script/server 


And you will see how the web server Mongrel starts to listen at "http://localhost:3000":rubystack-scriptserverIf you receive an error that says "Bad file descriptor - bind(2)" probably another server has already taken port 3000. Just launch our server in another one:

ruby script/server -p 3001 

Once the server is running (we are going to assume that it is in port 3000), you can point your browser to http://localhost:3000/ and see the welcome page.rubystack-browserMinimize the server window and open a new "Use Ruby" console, and move to the "projects/rubystack" directory again to keep working.



Our First Model


Suppose that we are going to make a web application that will keep track of all the music CDs we have, we will just need to store the album's name, and artist. 

Rails uses the MVC pattern, to illustrate how models work in Rails, we are going to generate one model called Album. To create a model which is going to be stored into the database you first need to create its corresponding table. In Rails, that is done through "migrations". Let's do it . Just run the following command:

ruby script/generate model album 

And you should see the output:

C:/Program Files/BitNami RubyStack/projects/rubystack>ruby script/generate model album
      create  app/models/
      create  test/unit/
      create  test/fixtures/
      create  app/models/album.rb
      create  test/unit/album_test.rb
      create  test/fixtures/albums.yml
      exists  db/migrate
      create  db/migrate/001_create_albums.rb


As you can see, Rails has generated a migration file for us:db/migrate/001_create_albums.rb, edit it and fill it with the following content:

class CreateAlbums < ActiveRecord::Migration 
  def self.up
    create_table :albums do |t|
      t.column :name, :string
      t.column :artist, :string
    end
  end

  def self.down
    drop_table :albums
  end
end


And from the "Use Ruby" console run:

C:/Program Files/BitNami RubyStack/projects/rubystack>rake db:migrate
(in C:/Program Files/BitNami RubyStack/projects/rubystack)
== CreateAlbums: migrating ====================================================
-- create_table(:albums)
   -> 0.0200s
== CreateAlbums: migrated (0.0200s) ===========================================


With that, rails has created the corresponding tables in our MySQL database. Now we need a way to read and write to that table, we need a Controller. And we need to provide an user interface for that Controller, we need a View. Fortunately Rails provide an automatic way (called scaffolding) of generating that stuff based in the table structure:

C:/Program Files/BitNami RubyStack/projects/rubystack>ruby script/generate scaffold album manage_albums
      exists  app/controllers/
      exists  app/helpers/
      create  app/views/manage_albums
      exists  app/views/layouts/
      exists  test/functional/
   dependency  model
      exists  app/models/
      exists  test/unit/
      exists  test/fixtures/
   identical  app/models/album.rb
   identical  test/unit/album_test.rb
   identical  test/fixtures/albums.yml
      create  app/views/manage_albums/_form.rhtml
      create  app/views/manage_albums/list.rhtml
      create  app/views/manage_albums/show.rhtml
      create  app/views/manage_albums/new.rhtml
      create  app/views/manage_albums/edit.rhtml
      create  app/controllers/manage_albums_controller.rb
      create  test/functional/manage_albums_controller_test.rb
      create  app/helpers/manage_albums_helper.rb
      create  app/views/layouts/manage_albums.rhtml
      create  public/stylesheets/scaffold.css

With a single command Rails has created a whole controller "app/controllers/manage_albums_controller.rb" with its six actions (new, show, edit, list, destroy and create) and their respective views under "app/views/manage_albums". Finally, you can see the results, just poit your browser to http://localhost:3000/manage_albums and enjoy adding some CDs to the database.rubystack-listingalbums

rubystack-newalbums


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值