RailsBack to Tutorials Ruby In Steel Tutorials Archives Rails
How To Create A Ruby On Rails Blog - Part Two
|IMPORTANT: To follow this tutorial, you should already have completed Part One of the Blog application.|
Generate The Comment Model
First, create the Comments model.
In the Generate window, select model as the Script Action, enter comment as the Script Value, ensure that the Generate radio button is selected and click Go.
After a few moments some more files will be added to the Solution Explorer.
Load into an editor the file app/models/comment.rb.
Edit the class definition to the following (i.e. this should now be the entire contents of this file)…
Edit this to:
Save all your changes (File, Save All).
Now create a Comments table in the database by using another migration.
Find the \db\migrate folder in the Solution Explorer.
Open the file 002_create_comments.rb in the editor.
Delete the existing self.up method and replace it with this:
The 002_create_comments.rb file should now look like this:
Save your changes
Now run the migration.
In the Rake window, select db:migrate as the action.
Shut the command window when the migration is complete.
Now let’s add comments to a view.
Right at the bottom, beneath the existing the code in this file, add this:
The contents of show.rhtml should now look like this...
Now, in blog_controller.rb, just under the existing index method, add this:
The file, blog_controller.rb should now contain this code at the top (plus many other lines of code which we don’t need to be concerned about)...
Save all the changes.
Try It Out
OK, now it’s time to see if it all works!
Press CTRL+W, W to display the browser (or use a standalone browser if you wish).
Make sure the address bar contains this (or using some other port number if you are not using the default, 3000):
As before, this is what you should see...
Click the New Post link.
This takes you to a post entry page.
Enter some Body text but leave the Title blank.
Click the Create button.
You will see an error:
This error occurs because the code
validates_presence_of :title in models/post.rb won’t allow us to save a post with no title.
Enter a Title for the post...
This will redisplay the list view, this time with your new post listed.
Click the hyperlinked name of the post.
This will take you to a comment form.
Enter a comment on the post.
Click the Comment button.
Enter another comment on the post.
Click the Comment button.
Notice that both comments are displayed under the original post:
Click the Back link to return to the list view.
|Note: This assumes that you have already started the One-Click Rails Debugger as explained earlier. If the debugger isn’t running, you will need to close the web server (if running) by selecting Debug, Stop Debugging; then start the One-Click Debugger.|
Let’s put a breakpoint on some code.
Open the file blog_controller.rb.
Click the margin to put a breakpoint (marked by a red highlight) on the following line of code in the def create method:
flash[:notice] = 'Post was successfully created.'
Open the browser (CTRL+W, W) Check that you are on the ‘list’ page. That is, the address should be similar to this:
Click the “New post” link.
In the ‘New Post’ page, enter a title and some body text.
Click the Create button
The debugger will stop on the line with the breakpoint. Let’s examine the @post variable. If it is not already visible, display the Watch window by selecting Debug, Windows, Watch, Watch 1 or pressing CTRL+D,W.
Highlight @post in the code editor and use the mouse to drag it into Watch window.
You will see @post listed in the Watch window. Note the + signs to its left. Click these to ‘drill down’ into the variable and examine its inner details.
You can also drill down into variables inside the editor itself. Just hover your mouse over @post in the editor. Note that a debug tooltip pops up. This too has + signs which can be clicked in order to view more details.
You can use all the other debugger features with Rails to trace through code, step into and over methods, navigate through the call stack and so on. For more information see: Debugging Rails With WEBrick or Mongrel; also refer to the manual for an in-depth tour of the debugger,
That is just a very simple example of creating and debugging a Ruby On Rails application using Ruby In Steel. I hope it provides you with some insight into ways in which Ruby In Steel can help you create and maintain applications all from within the Visual Studio environment.