A Rubyologist and a believer of 37Signal philosophy “always under do your competition” . Currently working as a Senior software engineer / Team Lead at Ridgecrest Asia (pvt) Ltd, leading several projects including www.ticketslk.com
 

How to create a sample blog using Ruby and Rails – Part 2.

11/25/2009 11:11 pm By Sameera Gayan | Articles: 9

Hi again.. I think you have already red my previous post about How to create a sample blog using Ruby (http://digit.lk/node/440), and now you are ready to explore rails further.

Before start off I would like to re-fresh your memory that we have already created a Blog where we can create posts. But for a blog publishing only Posts is not enough (isn’t it), we as blog owners like to know what other people thinking about out post , their comments...

So today in this lesson I will go through with you on how to enable readers to post comments about your blog post.

Earlier, in our first lessons we have drawn a small diagram on how our blog should looks like (relationships). so there we draw that each post might have 0 or more comments.

OK, quick guess? What we need next, Yes, and way to add comments, of course they should related to  a post. (in more technical terms Post has zero or more comments.)

So what are the properties we need in our comment object?

comment_description – comment

post_id – to which post this comment is for

id – which will create by rails automatically

Then we can use the “scaffold” as earlier,

open us your command prompt

browse to your project

type this command ruby script/generate scaffold Comment post_id:integer comment_description:text

press enter

that should create the necessary files for you, So lets run the migration to create the comments table, type

rake db:migrate

 

That should create the table for you, just to make sure start your project (ruby script/server) and browse this url

http://localhost:3000/comments and you should see the following

OK, now where shall we allow viewers to add comments, It should be when the user reading the post it self. So let’s see our post list. Go to

http://localhost:3000

(Remember to view the post list, you should at least add one post)... One more thing, I'm gonna slightly change our home page. Currently it has only the post title and I will be changing it to see the post content as well.

In

app/views/home/index.html.erb

I'll change this code

Isn't that simple? So now we have a page where we can see the posts list and their content. Now lets add a link for users to add comments, So I want the user to give a link, where once clicked it will direct to comments controllers new action to add comments.

We can add the link this,

<%= link_to 'Add comment', {:action => "new", :controller => "comments", :id => post.id } %>

Don’t worry.. Let me explain, this is a link to tag in rails, same as <a href=””></a> in normal html. 'Add comment' is the display of the link

:action => "new", :controller => "comments"– this will tell the destination of the URL

and what is this ? :id => post.id, remember we discusses that every comment have to have a post , (means when we are saving the comment, we have to save the post id as well.) So here we are passing our post id as a parameter. So our final view will looks like this (app/views/home/index.html.erb)

Let’s run our servers and see. When you click the 'Add comment' link you should be directed to comments add page, with the post id as a parameter.

Lets add the post id to our post id text box (in the view comments/new)

<%= f.text_field :post_id, :value => params[:id] %>

:value => params[:id] – line will add the post id as a value to the post_id text box.

Wait. Do we want our user to see the post_id in a text box, neah... I dont think so. Its better if we can hide it! Ok lets do it, Its not hard, we'll make the text box a hidden one..

<%= f.hidden_field :post_id, :value => params[:id] %>

OK, that make the post_id disappear, and Isn’t it cool that we show the user what is the post is.

For that, go to app/controllers/comments

and go to the action new and add the following, @post = Post.find(params[:id]), So that will create the post object for us.

 

And in the view (app/view/comments/new) add the following, <%= @post.post_name%>

Ok done, now you should be able to add comments to posts, Just try and see..
Opps.. did you notice that, once you add a comment it goes to comment show page and then goes to comments index.. Thats confusing.. What we want is, once you add the comment it should again come to home page...
So one little change is needed..


Go to app/controllers/comments and go to action create

change this line – redirect_to(@comment) to redirect_to root_url, and this will tell rails to redirect to home page instead of the comment show page.


Ok, whats next... Now we can create posts, and viewers can add comments. But is there a way of viewing comments, (if there are any...). No. So let’s start on that.
So my requirement is,
If a post has comments, number of comments should show and once user click on that he/she should be able to see all the comments related to that post.
Lets write a helper method for that, What ?!! helpers are handy tools in rails where you can write common methods to execute by the view. (Like getting the user name when given the user id etc.. Each controller has its own helper class and application_helper is the superior helper class which can be accessed by any view. -- OK next lesson will totally be theory on what is happening behind rails)

so open up, app/helpers/comments_helper.rb and add the following method

Its not complex, Let me explain,
Initially it has a  commentHTML – a blank string, then it gets all the comments for a given post by using

@comments = Comment.find_all_by_post_id(post_id)

and if there are posts (if @comments && @comments.length > 0) it create a link to view those posts (same as earlier)
and finally assign it to  commentHTML and return.
And one more change in  app/views/home/index.html.erb
Also, (to see the number of comments...)

Add this line - <%= comments(post.id)%>

OK just go to your home page and see,.. It should work, But wait, we might want to change the page a little (coz now it shows every comment..)
go to app/controllers/comments and go to index action

change this line – Comment.all to
Comment.find_all_by_post_id(params[:id])
This should show the comments only for a given post, and change the view also a to display the comments

Add this code to - app/views/comments/index.html.erb

OK, in here also you can add post title (like we did in comments/new) and try to add a new comment like to this page as well. (that is for your home work, :) )
When we meet next time, I'll show your the theory behind our actions, believe me rails is far more easy than you think..
So till the next issue, Happy programming.. ….

Previous Article

Share/Save
No votes yet