Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

"Add comments" guide #9

Merged
merged 2 commits into from
May 4, 2012
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion _posts/2012-04-18-app.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -251,5 +251,5 @@ root :to => redirect("/ideas")

## Additional Guides

* GUIDE 1: [Add commenting by Janika Liiv](http://janikaliiv.eu/homework/)
* GUIDE 1: [Add commenting by Janika Liiv](/commenting)
* GUIDE 2: [Put your app online with Heroku by Terence Lee](/heroku)
120 changes: 120 additions & 0 deletions _posts/2012-04-30-commenting.markdown
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
---
layout: default
title: Commenting functionality for the Rails Girls app
permalink: commenting
---
# Commenting for Rails Girls App
*Created by Janika Liiv, [@janikaliiv](https://twitter.com/janikaliiv)*

We are going to add the possibility to comment ideas in your *railsgirls* application.

The instructions for installing rails and building the ideas app can be found [here](/app)

## Step 1: Add foreigner gem

Add to Gemfile
{% highlight ruby %}
gem 'foreigner'
{% endhighlight %}

In your terminal stop the server if it's running and type
{% highlight sh %}
bundle install
{% endhighlight %}

## Step 2: Create comment scaffold

Create comment scaffold, with the commentator name, the comment body (contents of the comment) and with the reference to the ideas table (idea_id).
{% highlight sh %}
rails g scaffold comment user_name:string body:text idea_id:integer
{% endhighlight %}

## Step 3: Add foreign key connections
Add to migration the foreing key connection. Open db/migrate/ and the file, which name ends with 'create_comments.rb'. After
{% highlight ruby %}
t.timestamps
end
{% endhighlight %}

add
{% highlight ruby %}
add_foreign_key :comments, :ideas
{% endhighlight %}

Now migrate the database changes by typing in your terminal
{% highlight sh %}
rake db:migrate
{% endhighlight %}

start your server with:
{% highlight sh %}
rails s
{% endhighlight %}

## Step 4: Add relations to models

You need to make sure that Rails knows the connection 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 after the row
{% highlight ruby %}
class Idea < ActiveRecord::Base
{% endhighlight %}
add
{% highlight ruby %}
has_many :comments
{% endhighlight %}

The comment also has to know that it belongs to an idea.So open app/models/comment.rb and after
{% highlight ruby %}
class Comment < ActiveRecord::Base
{% endhighlight %}

add the row
{% highlight ruby %}
belongs_to :idea
{% endhighlight %}

## Step 5: Render the comment form and existing comments

Open app/views/ideas/show.html and after the image_tag
{% highlight erb %}
<%= image_tag(@idea.picture_url, :width => 600) if @idea.picture.present? %>
{% endhighlight %}

add
{% highlight erb %}
<h3>Comments</h3>
<% @idea.comments.each do |comment| %>
<div>
<strong><%= comment.user_name %></strong>
<br />
<p><%= comment.body %><p>
</div>
<% end %>
<%= render 'comments/form' %>
{% endhighlight %}

In app/controllers/ideas_controller.rb add to show action after the row
{% highlight ruby %}
@idea = Idea.find(params[:id])
{% endhighlight %}

this
{% highlight ruby %}
@comment = @idea.comments.new
{% endhighlight %}

Open comments/_form.html and after
{% highlight erb %}
<div class="field">
<%= f.label :body %><br />
<%= f.text_area :body %>
</div>
{% endhighlight %}


add the row
{% highlight erb %}
<%= f.hidden_field :idea_id %>
{% endhighlight %}
That's it. Now view an idea you have inserted to your application and there you should see the form for inserting a comment