Permalink
Browse files

Add comments to article.

  • Loading branch information...
1 parent a65c15f commit 089eac7077c0d51e30940c1e972f114bb75dc52e @fabioyamate fabioyamate committed Sep 25, 2011
@@ -13,3 +13,22 @@ div.well {
}
pre { display: none; }
+
+article .content {
+ margin: 10px 0;
+}
+
+#article-comments {
+ padding: 10px;
+ border: 1px solid #ccc;
+ background: #f5f5f5;
+ -moz-border-radius: 3px;
+ -webkit-border-radius: 3px;
+ border-radius: 3px;
+}
+
+#comments {
+ margin-bottom: 20px;
+ border-bottom: 1px solid #ccc;
+}
+
@@ -0,0 +1,24 @@
+class Articles::CommentsController < ApplicationController
+ clear_respond_to
+ respond_to :html
+
+ before_filter :find_article
+
+ def create
+ @comment = @article.comments.new(params[:comment])
+
+ respond_to do |format|
+ if @comment.save
+ format.html { redirect_to @article, :notice => 'Comment was successfully created.' }
+ else
+ format.html { render "articles/show" }
+ end
+ end
+ end
+
+ private
+
+ def find_article
+ @article ||= Article.includes(:comments).find(params[:article_id])
+ end
+end
@@ -13,7 +13,7 @@ def index
# GET /articles/1
# GET /articles/1.json
def show
- @article = Article.find(params[:id])
+ @article = Article.includes(:comments).find(params[:id])
respond_to do |format|
format.html # show.html.erb
@@ -1,3 +1,7 @@
class Article < ActiveRecord::Base
+ attr_accessible :name, :content, :'content_type'
+
+ has_many :comments
+
validates_presence_of :name
end
@@ -0,0 +1,7 @@
+class Comment < ActiveRecord::Base
+ attr_accessible :name, :body
+
+ belongs_to :article
+
+ validates_presence_of :name, :body
+end
@@ -2,19 +2,16 @@
<a href="#" class="btn" data-code="show">Show Code</a>
</div>
-<%= simple_form_for(@article, :wrapper => "inline") do |f| %>
+<%= simple_form_for @article, :wrapper => "inline" do |f| %>
<pre> simple_form_for(@article, :wrapper => "inline") do |f| </pre>
-
<% if f.error_notification %>
<div class="alert-message warning fade in" data-alert="alert">
<a class="close" href="#">×</a>
<p><%= f.error_notification %></p>
</div>
<% end %>
- <%= f.error_notification %>
-
<%= field_set_tag "Article" do %>
<%= f.input :name, :input_html => { :class => "span12" },
:hint => "add your article title here" %>
@@ -32,7 +29,6 @@
<div class="actions">
<%= f.button :submit, :class => 'primary' %>
<%= button_tag 'Cancel', :type => :reset, :class => "btn" %>
-
</div>
<pre>
f.button :submit, :class => 'primary'
@@ -0,0 +1,15 @@
+<%= simple_form_for [@article, @comment ||= Comment.new], :wrapper => "stacked",
+ :html => { :class => "form-stacked" } do |f| %>
+ <%= field_set_tag "Comment (stacked-form)" do %>
+ <% f.with_options :input_html => {:class => "xxlarge"} do |opt| %>
+ <%= opt.input :name, :hint => "please, identify yourself" %>
+ <%= opt.input :body, :hint => "add your comment here",
+ :input_html => { :rows => 4 } %>
+ <% end %>
+ <% end %>
+
+ <div class="actions">
+ <%= f.button :submit, :class => 'primary' %>
+ <%= button_tag 'Cancel', :type => :reset, :class => "btn" %>
+ </div>
+<% end %>
@@ -12,17 +12,31 @@
<article>
<header>
- <h1><%= @article.name %></h1>
+ <h1><%= @article.name %> <small><%= link_to "edit", edit_article_path(@article) %></small></h1>
</header>
<div class="content">
<%= @article.content %>
</div>
- <footer>
- <div class="well" style="padding: 5px;">
- <%= link_to 'Edit', edit_article_path(@article), :class => "btn" %>
- </div>
- </footer>
+ <section id="article-comments">
+ <h4>Comments</h4>
+ <ol id="comments">
+ <% @article.comments.each do |comment| %>
+ <li>
+ <article class="comment">
+ <div class="comment-name">
+ <strong><%= comment.name %></strong>
+ </div>
+ <div class="comment-body">
+ <p><%= comment.body %></p>
+ </div>
+ </article>
+ </li>
+ <% end %>
+ </ol>
+
+ <%= render 'articles/comments/form' %>
+ </section>
</article>
@@ -15,6 +15,17 @@
end
end
+ config.wrappers "stacked", :class => "clearfix", :error_class => :error do |b|
+ b.use :placeholder
+ b.use :label
+ b.use :hint, :tag => :span, :class => :'help-block'
+ b.use :tag => 'div', :class => 'input' do |input|
+ input.use :input
+ input.use :error, :tag => :span, :class => :'help-inline'
+ end
+ end
+
+
# Method used to tidy up errors.
# config.error_method = :first
@@ -40,7 +51,7 @@
# config.collection_value_methods = [ :id, :to_s ]
# How the label text should be generated altogether with the required text.
- # config.label_text = lambda { |label, required| "#{required} #{label}" }
+ config.label_text = lambda { |label, required| "#{label} #{required}" }
# You can define the class to use on all labels. Default is nil.
# config.label_class = nil
View
@@ -1,5 +1,7 @@
SimpleFormBootstrap::Application.routes.draw do
- resources :articles
+ resources :articles do
+ resources :comments, :only => :create, :controller => 'articles/comments'
+ end
root :to => 'articles#index'
end
@@ -0,0 +1,12 @@
+class CreateComments < ActiveRecord::Migration
+ def change
+ create_table :comments do |t|
+ t.string :name, :null => false
+ t.text :body, :null => false
+ t.references :article
+
+ t.timestamps
+ end
+ add_index :comments, :article_id
+ end
+end
View
@@ -11,7 +11,7 @@
#
# It's strongly recommended to check this file into your version control system.
-ActiveRecord::Schema.define(:version => 20110911190941) do
+ActiveRecord::Schema.define(:version => 20110925042740) do
create_table "articles", :force => true do |t|
t.string "name"
@@ -22,4 +22,14 @@
t.string "disabled_text"
end
+ create_table "comments", :force => true do |t|
+ t.string "name", :null => false
+ t.text "body", :null => false
+ t.integer "article_id"
+ t.datetime "created_at"
+ t.datetime "updated_at"
+ end
+
+ add_index "comments", ["article_id"], :name => "index_comments_on_article_id"
+
end
@@ -0,0 +1,11 @@
+# Read about fixtures at http://api.rubyonrails.org/classes/Fixtures.html
+
+one:
+ name: MyString
+ body: MyText
+ article:
+
+two:
+ name: MyString
+ body: MyText
+ article:
@@ -0,0 +1,7 @@
+require 'test_helper'
+
+class CommentTest < ActiveSupport::TestCase
+ # test "the truth" do
+ # assert true
+ # end
+end

0 comments on commit 089eac7

Please sign in to comment.