Funding for 'IT Lab' Project, Phase 1: Progress of sticker sales. Purchase a sticker to help us reach our target.Updated: 2010-02-28 11:53
10.7%
How to create a sample blog using Ruby.

By Sameera Gayan

If you have already heard of ruby/ ruby and rail, I’m sure the above title is very familiar to you. Yes, you read it right… We will create a Simple blog using ruby rails and explore the interesting points in rails… And oh.. If you are new to ruby and rails , our friend Lakshan has posted some great articles to start off with

Ruby for fun and profit  1 - http://digit.lk/ruby_february2009

Ruby for fun and profit  2 -http://digit.lk/09_april_ruby

Please note this post is highly focused on how to get a rails project up and running, faster and hence will not explain any theory behind them. But surely I’ll be explaining them in our next news letter.

So, let’s get started. But before jumping in to coding we will see what we want to get at. Here is a simple graphical representation of our blog.

OK… Now it’s time to start development.. Ah.. One last thing. This example uses following ruby and rails packages

ruby version - ruby 1.8.7  and rails version - Rails 2.3.3

And now we will start rolling… First we have to create our project, in my case I’m going to call it Blog

Open up a command prompt and type

rails Blog   - -database=mysql

Syntax - rails  <project name> -- database=<database you want>

This command will create a folder called Blog. Go to that directory

cd Blog

Now we have to edit our database configurations.

Open the config/database.yml file and set your database details.  Probably you might want to change the username and password

In the file you will find 3 sections

Development –

When your project runs in development mode. Rails will get the connection from this section.

Test

When you want to test, settings will load from this section

Production

When you want to go live , then  these settings will be used.

Now we will create the databases. Type this command

rake db:create:all

syntax : rake db:creat:<optional parameter>

    you may give only rake db:create which will create only the development database. But if you give the parameter ‘all’ rails  will create development, test and production databases for you.

OK..before going further.. we’ll check if our project is working. So lets start the rails server

ruby script/server

And if everything goes well you should be riding on rails. To check this open up a browser and type the following url

http://localhost:3000/

You should be getting the below screen, and to check furthermore, about your database connection, click on the “About your application’s environment” link. Rails will give you the basic details about your connection.

Cool, isn’t it. Now you have an up and running rails web server. So now we will develop something for create posts.

Concept –

Rails is based on Object Relational Mapping (ORM) ,  In ORM Objects will basically maps  to rows in a table. So here rails will create a model called Post and will create a table called posts.

Post -> posts

Now stop the web server – by passing ctrl + c and enter the following command

ruby script/generate scaffold Post post_name:string post:text

syntax

ruby script/generate scaffold  <model name>  <attribute>:<data type>..

Note the model name starts with a capital letter and singular (Post). And post_name and  post are attribute and we are telling rails about their data types also.

Now the magic should happen…. Rails  will create  all the necessary files to make your project up and running, quickly.

As per our initial plan, I will not going to explain what are these files now and surely I will in the next chapter. As I mentioned earlier, this will just be a quick practical example without going into the boring aspects, THEORIES...

 OK. Now we need to create the table (posts). Hold on… no need to go to mysql and create tables, rails has already created a script for you. Inside db/migrate. To create the table just pass this command

rake db:migrate

this should create the posts table for  you

now run the server again. ruby script/server and type the following url

http://localhost:3000/ posts

Did you get above screen ?!  Cool let’s start coding the functionality Add, Edit, Delete etc.. But wait… isn’t it rails. Yeah its rails… so why typing! ..  rails has  already  created those coding for you. Come’on you’ve got to believe me. Now you can

Add

Edit

Delete

View  posts .  Just click “New post ” link and see. You should be able to add posts, and those posts should be displayed in the above screen which allows you to edit or delete them. How cool is that?? But remember  its still just the beginning….

Just a quick overview on how this works.

inside your app folder

models – class implementation of the project

views – folder will have all the view’s rails will render you.

Controllers – will communicate with models and control the views

So now you have an up and running  blog site, you can maintain your posts.  Fine. Now what?!.. Quickly you will find out that you can create posts without even their names. Ah.. That’s not good ! we should validate that. Again remember we are on rails, so there should be a nice way of doing that (without writing so many codes for validation...) Yes, there is surely a way….

Open your model app/models/post

class Post < ActiveRecord::Base

   validates_presence_of  :post_name   

end 

validates_presence_of  :post_name - will validate the post name to check if it is blank. Not sure ! try to save a post with out giving a post name. It should validate.

Done. Now  there is a problem in our blog. That is to see your blog posts users need to go tohttp://localhost:3000/posts.

That’s not cool. (we should avoid  that /posts parameter) , then  it will be like http://localhost:3000/ and one day when you go live it will be like http://www.worldsbestblog.com.. Another thing would be we don’t want to show the edit, and destroy options to normal users.

So we will create another page and make it as our home page. Then all the public users will access that. Ah! That sounds good….isn’t it?, But where to start ? First we will create a controller called home and we need only one action “index” (that is the default action for any controller.)

ruby script/generate controller home index

syntax

ruby script/generate controller <controller name>  <action name>

to do this change, we need to do followings

open the /config/ routes.rb file and find the following line

# map.root :controller => "welcome"

And change the line as followings

map.root :controller => “home”

above code will tell rails to set the root URL to home controller as rails will get “index” action as default

next important step

go to /public/ and delete the index.html

and open the home controller in app/controllers

inside the index action add the following code

def index

@posts = Post.all  

end

this code will load all the posts

now open the view /app/views/home/index.rhtml and add this code

 

<h1>

   My Blog

</h1>

<ul>

  <%for post in @posts %>

   <li><%= post.post_name %></li>

  <%end%>

</ul>

 

restart the server

type

http://localhost:3000/ and you will see the posts list.

That’s it for this lessons. Next time we’ll see how to add comments to posts and creating relationships. And theories behind what we did.

And I hope you have seen how easy to develop a rails application.

 

And if you are interested we have created a Sri Lankan ruby rails user group to spread the word in Sri Lanka. There we have professional rails developers who are willing to help others at their very best.

http://groups.google.com/group/rubyologists

So, Please do try this by your own and try to explore the language.  It’s not hard, and if you have any queries drop me a line, but I would recommend posting them in the above group so that not only you get the expert’s advice the others can learn too.

And if you ever need the source code for this article, its here

http://github.com/sameera207/Rails-Lessons/tree/master/source

So till the next issue, Happy programming.. ….

 

 

 

 

 

 

Post new comment

  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.

More information about formatting options