Commenting for Rails Girls App
Created by Janika Liiv, @janikaliiv
We are going to add the possibility to comment on ideas in your railsgirls application.
The instructions for installing rails and building the ideas app can be found here.
1. Create comment scaffold
Create a comment scaffold, with the commentator name, the comment body (contents of the comment) and with the reference to the ideas table (idea_id
).
This will create a migration file that lets your database know about the new comments table. Run the migrations using
2. Add relations to models
You need to make sure that Rails knows the relation between objects (ideas and comments).
As one idea can have many comments we need to make sure the idea model knows that.
Open app/models/idea.rb
and below the row
add
The comment also has to know that it belongs to an idea. So open app/models/comment.rb
and below
add the row
3. Render the comment form and existing comments
Open app/views/ideas/show.html.erb
and after the image_tag
add
In app/controllers/ideas_controller.rb
add to the show method
Open app/views/comments/_form.html.erb
and after
add the row
next, remove
That’s it. Now view an idea you have inserted to your application and there you should see the form for inserting a comment as well as deleting older comments.
Other Guides
- Handy cheatsheet for Ruby, Rails, console etc.
- Guide 1: Guide to install Rails
- Guide 2: Build Your First App
- Guide 3: Push Your App to GitHub
- Guide 4: Put your app online with…
- Guide 5: Allow Comments on Your App (Current page!)
- Guide 6: Add design using HTML & CSS
- Guide 7: Create thumbnails with Carrierwave
- Guide 8: Add Authentication (user accounts) with Devise
- Guide 9: Add Profile Pics with Gravatar
- Guide 10: Improve your design with HTML and CSS
- Guide 11: Continuous Deployment
- Guide 12: Build a voting app in Sinatra
- Guide 13: Build a diary app in Ruby on Rails
- Guide 14: Add a back-end to your app (admin pages)
- Guide 15: Go through additional explanations for the App