Allow Comments On Your App
Created by Janika Liiv, @janikaliiv.
Compiled and modified for Rails Girls KL by Faezrah, @fzrhrs.
This guide assumes that you have already built a Rails Girls app by following the extended app guide and you've added user authentication to your app.
1.Create Comment Model
Create a comment model, with the comment body (contents of the comment), reference to the photos table (photo_id) and also reference to the users table (user_id).
This will create a migration file that lets your database know about the new comments table. Run the migration with:
2.Associating Models
You need to make sure that Rails knows the relation between objects (photos, comments and users). As one photo can have many comments we need to make sure the photo model knows that. Open app/models/photo.rb
and after the line:
Add:
Add the same relation in app/models/user.rb
.
The comment also has to know that it belongs to a photo and a user. So in app/models/comment.rb
, after the first line, add the following:
Resource: Learn more about Active Record Associations.
3.Create Comment Controller
Now, we need a controller for comments.
We will then need to add a route so that Rails knows where we would like to navigate to see comments.
In config/routes.rb
, edit it as follows:
This creates comments as a nested resource within photos.
Resource: Learn more about Rails Routing.
4.Render The Comment Form And Existing Comments
Like Instagram, users may add their comments after seeing the photo, and once they have added their comment, they will be sent back to the photo show page to see their comment.
To do this, we need to add a form for comments in the app/views/photos/show.html.erb
, like so:
Your comments_controller.rb
should have something like this:
To display existing comments for a specific photo, we need to adjust our app/views/photos/show.html.erb
. After:
You can add:
That's it.