Topic: HowTo Create a Rails Blog in 15 steps without nested resources + AJAX
The first 10 steps tipically used for every Rails Application.
The steps 10-15 make your Blog functional.
If you choose to use AJAX follow additional steps from 16-20.
1.- rails blog -d mysql
2.- cd blog
3.- Edit your config/database.yml
4.- rake db:create:all
5.- Lets use the generators !!!
ruby script\generate scaffold Post title:string body:text
ruby script\generate scaffold Comment body:text post:references
6.- rake db:migrate
7.- In config/routes.rb: map.root :controller => "posts"
8.- Delete public/index.html
9.- Edit the post & comments model.
[app/models/post.rb]
class Post < ActiveRecord::Base
has_many :comments
end[app/models/comment.rb]
class Comment < ActiveRecord::Base
belongs_to :post
end
10.- ruby script\server
http://127.0.0.1:3000/posts
Create a couple Posts
Helo Rails 2.0.2
My Rails 2.0.2 Blog
All this is pretty standar procedure creating Rails Applications.
11.- Create a Partial file called [ _show_comments.html.erb ] in [ app/view/posts/ ]
You can copy the file from [ app/view/comments/index.html.erb ]
cp app/view/comments/index.html.erb app/view/posts/_show_comments.html.erb
11b.- Edit the file [ app/view/posts/_show_comments.html.erb ] and modify the line:
OLD <% for comment in @comments %> to NEW <% for comment in @post.comments %>
This will use the asociation between Post/Comments
11c.- Delete the last line:
<%= link_to 'New comment', new_comment_path %>
We will insert another partial to do this in the same page.
12.- Lets include the partial in the show post file to show the comments for that particular Post !!!
Edit [ app/view/posts/show.html.erb ] add to the end of the file:
<%= render :partial => "show_comments" %>
Browse to the show Post and you should be able to see the list of comments for each Post.
NOTE: Unless you use the console at this time there is no comments in our Blog. as you complete the remaining steps you will be able to do it.
13.- Create a Partial file called [ _new_comment.html.erb ] in [ app/view/posts/ ]
You can copy the file from [ app/view/comments/new.html.erb ]
cp app/view/comments/new.html.erb app/view/posts/_new_comment.html.erb
13b.- Edit the file: [app/view/posts/_new_comment.html.erb]
Change line:
<% form_for(@comment) do |f| %>
to:
<% form_for :comment, @comment, :url => comments_path do |f| %>
13c.- Change our reference to the post_id using the @post.id value
:
<p>
<b>Post</b><br />
<%= f.text_field :post %>
</p>to:
<p>
<%= f.hidden_field "post_id", :value => @post.id %>
</p>
13d.- Delete the last line:
<%= link_to 'Back', comments_path %>
Is not necesary since we always use the Show/Post page.
14.- Lets include the partial in the show post file to show the form to enter comments for that particular Post !!!
Edit [ app/view/posts/show.html.erb ] add to the end of the file:
<%= render :partial => "show_comments" %>
15.- At this point the Comments form is fully functional, but lets modify the redirect to back to the same page.
Edit the file [app/contorllers/comments_controller.rb]
In the create method change:
#format.html { redirect_to(@comment) }
format.html { redirect_to(:controller => 'posts', :action => 'show', :id => @comment.post_id ) }
This will automatically redirect to the specific Post when we add our comment !!!.
That's all folks !!!
Feel free to modify your views, stylesheets & layouts to fit the look & feel of your blog.
Modify the _show_comments.html.erb partial to show the title of the post.
<%=h comment.post %> to <%=h comment.post.title %>
The _show_comments partial can show the number of Comments if you change the first line:
<h1>Listing comments (<%= @post.comments.size %>)</h1
This Tutorial is not ready to deploy in a production server is just for learning purposes. please look into:
a) RoR 2.0.2 :: Very simple authentication from scratch in 15 steps
http://railsforum.com/viewtopic.php?id=15130
b) HowTo - Advanced MD5 database authentication from scratch in 20 steps.
http://railsforum.com/viewtopic.php?id=17512
c) Perhaps check into some of the rails plugins to make it secure !!!.
Keep in mind the comments URL still accessible, needs to be secured too.
* AJAX functionality is an option
The recipe should add few more steps like:
16.- Lets add some AJAX
Edit the file [ app/views/layouts/posts.html.erb ] add to the <head> section:
<%= javascript_include_tag :defaults %>
17.- Lets update the _new_comment.html.erb partial replacing form_for with remote_form_for:
<% remote_form_for :comment, @comment, :url => comments_path do |f| %>
18.- When the submit button send the request is going to expect JavaScript code to be returned. So we need to update the respond_to block to
respond appropriately [app/controller/comments_controller.rb]
format.js # renders create.js.rjs
19.- Create the [app/views/comments/create.js.rjs]
page[:show_comments].replace_html :partial => 'posts/show_comments',
:locals => {:post => @post}
20.- Create the <div> with id="show_comments" in the show posts page:
<div id="show_comments">
<%= render :partial => "show_comments" %>
</div>
Once you save your changes you should be able to add comments and the portion of the page with the div="show_comments" should be refreshed. I
I might be missing some little detail. The new comment post succeed but does not update the page. I'm trying to debug with Firebug.
Any suggestion is welcome !!!. But you got the idea ![]()
This is the error returned from Firebug and the same reported in the development.log ![]()
########################################
<p>
Showing <i>posts/_show_comments.html.erb</i> where line <b>#9</b> raised:
<pre><code>You have a nil object when you didn't expect it!
The error occurred while evaluating nil.comments</code></pre>
</p>
<p>Extracted source (around line <b>#9</b>):
<pre><code>6: <th>Post</th>
7: </tr>
8:
9: <% for comment in @post.comments %>
10: <tr>
11: <td><%=h comment.body %></td>
12: <td><%=h comment.post.title %></td>
</code></pre></p>
########################################
Dinooz.
Brains R like Books only work when they R Open.