|
|
||
Brian Leonard's BlogRails and Model RelationshipsPosted by bleonard on April 27, 2007 at 05:33 PM | Comments (1)Here we enhance our Ruby web log so that readers can add comments. In the process we will learn how to deal with model relationships in Rails, as a single post can have zero or more comments. Setting Things UpI'm going to begin from where I left off in my previous post: Rails and Database Migrations. Alternatively, you can start from BlogDemo.zip, which is the completed project from that post. Test the BlogDemo Project
Create the Comment Model
Define the Model Relationships
Regenerate our Controller ScaffoldingWhen we first created this project and generated the scaffolding for the Post model, we didn't choose to overwrite the existing blog controller, so the scaffolding actions were not created. So that we can customize the scaffold actions, we will delete the blog_controller.rb and generate the scaffolding again. Note, we are choosing not to use the overwrite option because our views have been customized and we we don't want to loose those customizations:
Create a New Action for Posting CommentsWe're going to modify the show view to include a text area so comments can be added. First we're going to create a new action for posting the comments.
Modify the View to Post Comments
Test
Displaying the CommentsOur blog doesn't yet display the comments that have been added, let's fix that.
Test
The Completed ApplicationBookmark blog post: CommentsComments are listed in date ascending order (oldest first) | Post Comment
| ||
|
|