Subscribe to NotarySojac's debian career        RSS Feed
-----

Day 6.3 - Rails application!

Icon Leave Comment
This post is a part of a mini-series!
Day 6
1) Installing  link
2) Test a Rails App! (You Are Here)
3) Configuring Webserver link
4) Configuring MySql link


Note: This entry is based on rails version 3.0.9. To maximize your compatibility, you might want to use RVM to install that exact version... But as long as you use rails 3.x.x, things will probably work out just fine.

Spoiler


Finally we made it! We setup our debian machine, and can test a rails application out. This entry entails my first rails creation. Looks like we'll have a few more entries in this blog that explain how to setup a rails app using an alternative database engine, web server, and involve virtual hosts. But not in this episode. No, today we just make a plain simple rails application.


Rails is a web site design framework that allows a programmer to rapidly create
A web place with persistent data access for the end user via the latest in
database technologies.

To say "hello" in rails, we're going to make a url direct us to a simple web page...
And that simple web page will let us type inputs into it and it will store whatever
we type. It will be our duty as end users to type the words "hello world" rather
than we hard code them (after all, it wouldn't make sense to just show a static,
"hello world" in a web browser, since that's NOT what rails is for, rather, rails
is for allowing web site visionaries to work with database-oriented web applications.




Step 1) make a new rails application


First we gotta set our system to explicitly use ruby version 1.8.7, because everytime it updates (they just updated a couple days ago) everyone's rails procedure changes... Ok, I'm updating this tut to use ruby ruby-1.9.3 (p194) to be trendy. But the message is still important, being on the bleeding edge can be prohibitively taxing.
$  rvm install 1.9.3
$  rvm use 1.9.3
$  gem install rails -v=3.2.0


That command not only switches our ruby -v to be 1.9.3, but it also automatically switches our gem version to 1.8.5 because the two softwares are so interrelated. You may have to reinstall your gems for this version if you only have them installed for another.


After that hassle is over with, we can create a new rails application.
$  rails new myapp
$  cd myapp
$  bundle install # this installs dependancies, and is automatically run for version 3.2.x so redundant


This powerful one liner will make tons of fantastic files in a directory named "myapp".
These files all work in tandem to provide a thrilling, database experience across the web.
Just imagine all the amazing things we can do with these rails files we just created.
* We could produce a journalistic report on a recent headline (in a day!? by 6:00PM!?!? how amazing! And users could contribute input, and someone could administrate the story so ppl who want to follow stories will know exactly where to come to in order to see the latest developments! This is an unfilled niche at the moment actually!)
* We could create a social network to spy on members of the population and leak the details to the highest bidders (unfortunately, this market is cornered already...)
* We could create a website that allows people to buy stupid crap to make themselves feel whole (or less half as they are) in the society they're quite bound to.
* We could make a website for me to keep my flashcards on.
* We could develop a web application that allows me to store my notes online.

The sky is the limit with rails.


Step 2) Setup the database
$  rails generate scaffold Hello hello:string


Running that command will create "/myapp/db/migrate/Hello.rb"
Open it up to see how it setup your databases using the active record statements (it's easy):

(vi db/migrate/create_hellos.rb)
class CreateHellos < ActiveRecord::Migration
  def self.up
    create_table :hellos do |t|
      t.string :hello

      t.timestamps
    end
  end

  def self.down
    drop_table :hellos
  end
end


Importantly, it also made...
app/models/hello.rb                  // "gives life" to your database
test/fixtures/hellos.yml             // not sure what a fixture is yet...
                                     // added a line to routes.rb "resources :hellos"
app/controllers/hellos_controller.rb // "gives life" to url paths
app/views/*...                       // determines the user interface when some1 browses to your app's model





Then instantiate the database (everything in the migrate folder):
$  rake db:migrate


That will have made all the database tables and columns that we specified in
create_database.rb


Step 3) Observe your work
$  rails server


That will start rails' built in web server, WEBrick. It sucks, but it's auto-functional.
When you have that running, paste the below code into a web browser of your choice.
http://localhost:3000/hellos


That will take use to our web application localhost (running on port 3000) and specifically,
to our hello's 'view'. There, we will see that we can create a New Hello by clicking a link.
Once directed to localhost:3000/hellos/new you should see an input box.
Carefully type "Hello world!" into it and hit enter.
We did it! We did a hello world in rails.
You can repeat the procedure by going 'back' to localhost:3000/hellos to create many hellos!
They're all being stored in an sqlite database! This is amazing! To do this in raw PHP takes...
Well, actually to do it in PHP it's not so bad, but we'll see in following tutorials why rails is so relatively powerful.










Revelation:

You can now make changes to the database from the web interface we created!
Play around with it. It should let you create new items, and even delete them.

At this point, it's pretty ugly, and pretty boring, and we aren't even talking about the .rb files that ruby automatically generated for us by our commands!
But if I type anymore in this tutorial, it will become too long, and too boring... And now simply isn't the time for that, we should continue tweaking our system until it is powerful and lean. Are you eager to do just that?

0 Comments On This Entry

 

Trackbacks for this entry [ Trackback URL ]

There are no Trackbacks for this entry