August 24th, 2008
Tutorial : Create a blog using Ruby on Rails 2. (Part 1 – Relationships)
Hello !
Its been a long time since my last post. I have been hell busy this week, barely had time to update. This is the revised version.
This tutorial will form the base for the upcoming series of tutorials on how to create a simple blog application in Ruby on Rails(RoR).
Part 1 will discuss about how relationships are defined in RoR and the basics as usual
Index
- Page 1 : Introduction
- Page 2 : Create Scaffolds application
- Page 3 : Modifying the Model,Controller and View
Introduction
Let me explain what we are trying to create. We will be creating our own very simple blog application using Ruby on Rails. The blog will have
- A Simple Login System
- A Simple Administrative Interface to manage posts,categories and comments
- Display comments for every post
- Ajax Implementation
What all you`ll learn :
- To create and work with scaffolds
- Relationships in Rails
- Migrations
- How Models, Controllers and Views work
- A simple login system
- Ajax Implementation in this blog
A lot more as we go on …
Some Conventions in Rails
-
The name of the table must be in its plural form: By default everytime we create a scaffold, the table created will be of its pluralized form. Rails will derive the singular name for the corresponding class automatically.
For Example. If we create a scaffold called category, Rails will automatically derive its plural form i.e categories. - By default primary key called id will be created for every scaffold even if not specified. The Data type of the Primary key should be Integer.
-
The Foreign keys should be named ‘table name of the referenced table + _id’
For Example : if we want to use Table-Category’s Primary key ‘id’, we`ll create a attribute called category_id in our ‘posts’ table.
What are we doing in Part 1 ?
In this tutorial (Part 1 of the Series) we`ll learn how to create different categories and assign our posts to these categories. Every post will show which category it belongs to.
So to achieve this, we need 2 tables. The database schema for the same will be something like
category(id,category_name)
post(id,post_title,post_text,date_of_creation,category_id)
The schema is pretty self explainatory. category_id of ‘post’ is the attribute which will contain the id of ‘category’. The simplest way to put this is, every record inside the post table will contain category_id which will help us retrieve the category_name by referring to the other table i.e category.
Pre-requistes :
I would really suggest you people to read the following tutorials, to understand the basic of RoR.
- Please have a developer environment set up. For more information on how to set it up, read
Tutorial : Installing Ruby on Rails 2 in Windows Vista/XP - Tutorial : Developing your First Ruby on Rails 2 Application
- Tutorial : Basics on Ruby on Rails 2 Model, Controller and Views and Routing















March 12, 2010 at 6:15 pm
All your links come to a 'nothing found' page
February 28, 2010 at 12:37 pm
Hi There,
Great tutorial, I'm just getting into Ruby on Rails and really wanted to get my own blog platform up and running asap, so this is helping loads!
One thing I noticed, when you modify the category selection box to become an option field rather than a text box I believe you can do it with one line as follows:
<%= f.collection_select :category_id, @cat, :id, :category_name %>
That uses the @cat variable from the controller and handles the looping for you, I picked it up from the apple tutorials on RoR and just thought it was worth mentioning as it is a little more lightweight and elegant solution
Moving on!
Adam
July 11, 2009 at 8:02 am
Love the captcha!
Anyway why is grant being an ass? If ya gunna critize it, ya gotta constructify it, brother! How should this article be better? Can you write an article for us that does this but better? Like “The best freaking article on creating categories in Ruby on Rails!!”
I got a question though, how do I know the relationship has been formed? How do I know the project knows Posts belongs to Categories, and Categories has many Posts?
March 31, 2009 at 9:37 pm
Hi ! I’ve found some errors. Maybe I am wrong, but I had many errors before I noticed that closing brackets for ruby code in .erb files located behind comment sign (#), so operators were just not closed because of that. Since I moved closing brackets (“%>”) from behind the commentaries to the right part, before them, everything seem to work. Hope it will help somebody else too.
And anyway, great tutorial!
March 16, 2009 at 10:39 pm
it’s just great. thanks for what you are doing. it helped me a lot.
March 4, 2009 at 6:53 pm
Very useful tutorial! Thanks a lot!
November 19, 2008 at 1:06 am
I change the tables name but i follow all step in the tutorial, but give me this error mensage:
NoMethodError in Checkdates#show
Showing checkdates/show.html.erb where line #13 raised:
undefined method `room’ for #
Extracted source (around line #13):
10:
11:
12: roomtype:
13:
14:
15:
16: Adults:
I don’t know how to solve this problem, please who can help me!
November 19, 2008 at 12:56 am
I try it but give me this error
NoMethodError in Checkdates#show
Showing checkdates/show.html.erb where line #13 raised:
undefined method `room’ for #
Extracted source (around line #13):
10:
11:
12: roomtype:
13:
14:
15:
16: Adults:
Can you help me please
November 18, 2008 at 10:48 am
@Dave Porter
Sorry coudln`t reply earlier. Dave could you temme what the error is ? It`ll be a lil helpful for me to decipher.
November 17, 2008 at 8:12 am
Still errors in code
( e.g. the category loop in the new and edit views section
cheers Dave
November 9, 2008 at 8:55 pm
Hey Mac,
Sorry couldn`t reply earlier.
RoR performs all joins automatically based on the attribute we tell it to perform join on. BTW Mac please use
[sourcecode]
[code] -- ALL YOUR CODE HERE -- [/code]
[/sourcecode]
Regards
November 8, 2008 at 10:05 pm
dude, I just posted some html in your comment block and broke it…sorry… on a more serious note, iframe/sql injections are going around these days, nasty buggers…you might want to do some input checking or at least install a picture/person verifier so bots dont inject your blog with nastiness.
November 8, 2008 at 10:01 pm
Sure enough, I tried it and got this error…help?!
Showing posts/index.html.erb where line #16 raised:
You have a nil object when you didn’t expect it!
The error occurred while evaluating nil.category_name
Extracted source (around line #16):
13:
14:
15:
16:
17:
18:
19: ‘Are you sure?’, :method => :delete %>
November 8, 2008 at 9:28 pm
Um, could someone explain to me how index.html.erb in step 3.4 is aware of the variable @post.category.category_name? It looks like its some kind of inner join with the records in the category table… thoughts?
November 6, 2008 at 11:42 am
Fantastic help, thanks!
September 12, 2008 at 3:27 am
I had to remove the comments in the changes to new.html.erb to make it work:
Category
<option value=”">
September 8, 2008 at 9:55 am
Whats the schedule for this tutorials? Keep them coming!
September 5, 2008 at 2:17 am
Nice write up. I used it to get up to speed on the changes in scaffold in RoR2. Thanks for that. I’m looking forward to the second part.
August 25, 2008 at 6:54 pm
Hey grant,
Thanks for the reply, I guess I should have taken my time in preparing this tutorial. I rushed up in a bid to finish it off fast.
Anyways I have posted a revised version which is I hope is better than the previous version. I`ll be constantly updating it.
Thanks for pointing out. I`ll see to it, that it won`t happen next time. This site is dedicated to provide the best content.
Regards
August 25, 2008 at 2:10 am
Poorly formatted and lacks clear description. A tutorial should be a constructive narrative, not a terse recipe.
My favorite bit from the tutorial is this hunk of meaninglessness: “Create some categories and then try creating posts. You`ll see that we have got our final result.”
Yeah, you got a final result all right.