<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/css" href="/stylesheets/rss.css"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/">
  <channel>
    <title>NextLogic Singapore Blog: Ruby on Rails Kickstart</title>
    <link>http://blog.nextlogic.net/articles/2007/09/07/ruby-on-rails-kickstart</link>
    <language>en-us</language>
    <ttl>40</ttl>
    <description></description>
    <item>
      <title>Ruby on Rails Kickstart</title>
      <description>&lt;p&gt;So the first public training is over now. I've learned a couple of things. I think this was one of the most difficult trainings I've ever been through &#8211; and even though it seems better after getting some distance there was definitely a few things we could have done better. What I really learned here is that even though Ruby on Rails is easy, it may be not so easy and really helps if you have a programming experience.&lt;/p&gt;

&lt;p&gt;In any case here are the steps we covered in the first session. The whole point was to just get a basic scaffold running. There was a time I thought it was easy. I don't anymore :-).&lt;/p&gt;
&lt;ol&gt;
	&lt;li&gt;
Install everything - &lt;a href="http://resources.nextlogic.net/ror/"&gt;here's one&lt;/a&gt; way to do it on Windows - it worked on most of the machines but not all. Things to watch out for: in few cases ruby bin dir wasn't added to the path, in others Mysql wouldn't start yet in others Apache wouldn't start. Most of it were XP machines but I guess there were differences. As pretty much all of our development is done on Linux and MacOS I just have more experience with those platforms.
	&lt;/li&gt;
	&lt;li&gt;
		What you will need - &lt;b&gt;2&lt;/b&gt; command prompt windows, text editor, mysql (or any other database) and browser
	&lt;/li&gt;
	&lt;li&gt;
Create a directory for your applications - on Windows type this:
&lt;pre&gt;
cd c:\
mkdir rails
cd rails
&lt;/pre&gt;
&lt;br/&gt;
&lt;img src="http://resources.nextlogic.net/blog/images/win_1.jpg" /&gt;
&lt;br/&gt;&lt;br/&gt;
and here it is for MacOS / Linux:
&lt;pre&gt;
cd ~
mkdir rails
cd rails
&lt;/pre&gt;
&lt;br/&gt;
&lt;img src="http://resources.nextlogic.net/blog/images/mac_1.jpg" /&gt;&lt;br/&gt;&lt;br/&gt;
	&lt;/li&gt;
	&lt;li&gt;
Generate the basic application structure:
&lt;pre&gt;
rails test
&lt;/pre&gt;
You should see a bunch of lines getting generated:&lt;br/&gt;
&lt;img src="http://resources.nextlogic.net/blog/images/rails_generating.jpg" /&gt;
&lt;br/&gt;
As a result you should be able to see the following directory structure generated:&lt;br/&gt;
&lt;img src="http://resources.nextlogic.net/blog/images/folder_structure.jpg" /&gt;
	&lt;/li&gt;
	&lt;li&gt;
Change to the generated directory and start the server to see if we're good to go:
&lt;pre&gt;
ruby script/server
&lt;/pre&gt;
You should see something like this - confirming that the server has been started:&lt;br/&gt;
&lt;img src="http://resources.nextlogic.net/blog/images/script_server.jpg" /&gt;
&lt;br/&gt;
Now open up a browser and navigate to http://localhost:3000. You should see this page coming up:&lt;br/&gt;
&lt;img src="http://resources.nextlogic.net/blog/images/index.jpg" /&gt;
&lt;br/&gt;
To stop the server you can press &lt;b&gt;Ctr+C&lt;/b&gt;. To start it again just press arrow up which should repeat the last command (i.e. ruby script/server).
	&lt;/li&gt;
	&lt;li&gt;
		&lt;h3&gt;From now on we will be using the another command line window. Leave the server running in the first window.&lt;/h3&gt;
	&lt;/li&gt;
	&lt;li&gt;
		Set up the database connection. The database connection parameters are set up in &lt;i&gt;config/database.yml&lt;/i&gt; file. On most platforms it should be geneated properly,
		but on some you may have to add the &lt;i&gt;host: localhost&lt;/i&gt; line. At the end it should look something like this&lt;br/&gt;
		&lt;img src="http://resources.nextlogic.net/blog/images/database_yml.jpg" /&gt;
		&lt;br/&gt;
	&lt;/li&gt;
	&lt;li&gt;
		Now we can create a new database. Type this in the second command prompt window:
		&lt;pre&gt;
			mysql -u root 
			create database test_development;
			create database test_test;
			\q
		&lt;/pre&gt;
		You should see something like this:&lt;br/&gt;
		&lt;img src="http://resources.nextlogic.net/blog/images/mysql_create_database.jpg" /&gt;
		&lt;br/&gt;
		To verify that everything is okay run &lt;i&gt;rake db:migrate&lt;/i&gt; and you shouldn't see any error messages. If you see warning about require_gem being obsolete - it's okay.
	&lt;/li&gt;
	&lt;li&gt;
Now we generate some basic model:
&lt;pre&gt;
ruby script/generate model product
&lt;/pre&gt;
&lt;br/&gt;
&lt;img src="http://resources.nextlogic.net/blog/images/product_model.jpg" /&gt;&lt;br/&gt;
	&lt;/li&gt;
	&lt;li&gt;
Create a Migration&lt;br/&gt;
I will get to the whole migration concept in some future installments - for now just open up db/migrate/001_create_products.rb and add the following lines:&lt;br/&gt;
&lt;pre&gt;
class CreateProducts &lt; ActiveRecord::Migration
  def self.up
    create_table :products do |t|
      t.column :name, :string
      t.column :description, :text
      t.column :price, :numeric
      t.column :image_url, :string
    end
  end

  def self.down
    drop_table :products
  end
end

&lt;/pre&gt;
	&lt;/li&gt;
	&lt;li&gt;
Now the hard part - run the migration. Running the migration will make the changes in the database. You will HAVE to have your database as well as database.yml properly set-up, otherwise this won't work.
&lt;pre&gt;
rake db:migrate
&lt;/pre&gt;
If everything went well you should see something like this:&lt;br/&gt;
&lt;img src="http://resources.nextlogic.net/blog/images/run_migration.jpg" /&gt; 
&lt;br/&gt;
	&lt;/li&gt;
	&lt;li&gt;
Create a controller.
&lt;pre&gt;
ruby script/generate controller products
&lt;/pre&gt;
Just like always, you should see some lines informing you about the files being generated. Here are the new files that got generated:
&lt;br/&gt;
&lt;img src="http://resources.nextlogic.net/blog/images/products_controller.jpg" /&gt; 
&lt;br/&gt;
	&lt;/li&gt;
	&lt;li&gt;
		Now just add this line to the generated controller so that it looks like this:
		&lt;pre&gt;
class ProductsController &lt; ApplicationController
  scaffold :product
end
		&lt;/pre&gt;
	&lt;/li&gt;
	&lt;li&gt;
		And here we are :-) Navigate the browser to http://localhost:3000/products/list and you should see this:
		&lt;br/&gt;
		&lt;img src="http://resources.nextlogic.net/blog/images/products_list.jpg" /&gt; 
		&lt;br/&gt;
		Go ahead and click on New and add a new product:&lt;br/&gt;
		&lt;img src="http://resources.nextlogic.net/blog/images/products_new.jpg" /&gt;
		&lt;br/&gt;
		
	&lt;/li&gt;
	&lt;li&gt;
		Now we've got the rice (or meat and potatos if you're from that culture) - now just make it look better. We'll add a very basic layout and some basic CSS.
		Both of them are from the Agile Web Development with Ruby on Rails book examples. You need to create a new file under app/views/layouts/application.rhtml and add the following to it:
		&lt;br/&gt;
		&lt;img src="http://resources.nextlogic.net/blog/images/application_rhtml.jpg" /&gt;
		&lt;br/&gt;&lt;br/&gt;
		&lt;a href="http://resources.nextlogic.net/blog/images/depot.css"&gt;Get the Style Sheet Here&lt;/a&gt;
		&lt;br/&gt;
		And here's the end product:&lt;br/&gt;
		&lt;img src="http://resources.nextlogic.net/blog/images/products_list_style.jpg" /&gt;&lt;br/&gt;
	&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;And that's all. Easy or not - if you can do this you will be able to do anything. If you were not able to follow or the whole darn thing just wouldn't work - let me know where you had troubles.&lt;/p&gt;

</description>
      <pubDate>Fri, 07 Sep 2007 09:33:00 -0400</pubDate>
      <guid isPermaLink="false">urn:uuid:1676259f-e082-45f9-b894-007436a70c45</guid>
      <author>Peter Bohm</author>
      <link>http://blog.nextlogic.net/articles/2007/09/07/ruby-on-rails-kickstart</link>
      <category>Ruby</category>
      <category>Rails</category>
      <category>Programming</category>
    </item>
  </channel>
</rss>
