Skip to content

Instantly share code, notes, and snippets.

@ujihisa
Created May 25, 2009 22:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ujihisa/117793 to your computer and use it in GitHub Desktop.
Save ujihisa/117793 to your computer and use it in GitHub Desktop.
diff --git a/app/controllers/comments.rb b/app/controllers/comments.rb
index 531ea2f..7c59fab 100644
--- a/app/controllers/comments.rb
+++ b/app/controllers/comments.rb
@@ -26,9 +26,10 @@ class Comments < Application
end
def create(comment)
- @comment = Comment.new(comment)
+ @post = Post.get(params[:post_id])
+ @comment = Comment.new(comment.merge(:post => @post))
if @comment.save
- redirect resource(@comment), :message => {:notice => "Comment was successfully created"}
+ redirect resource(@post), :message => {:notice => "Comment was successfully created"}
else
message[:error] = "Comment failed to be created"
render :new
diff --git a/app/models/comment.rb b/app/models/comment.rb
index 02c44cc..c6b3a23 100644
--- a/app/models/comment.rb
+++ b/app/models/comment.rb
@@ -7,4 +7,5 @@ class Comment
property :body, String
property :post_id, Integer
+ belongs_to :post
end
diff --git a/app/models/post.rb b/app/models/post.rb
index 7201274..b49ccb1 100644
--- a/app/models/post.rb
+++ b/app/models/post.rb
@@ -6,4 +6,5 @@ class Post
property :body, String
property :title, String
+ has n, :comments
end
diff --git a/app/views/comments/_comment.html.haml b/app/views/comments/_comment.html.haml
new file mode 100644
index 0000000..7e54ce8
--- /dev/null
+++ b/app/views/comments/_comment.html.haml
@@ -0,0 +1,6 @@
+%p= error_messages_for :comment
+
+= form_for(:comment, :action => "/posts/#{@post.id}/comments") do
+ %p= text_field :name, :name => "comment[name]"
+ %p= text_area :body
+ %p= submit "Comment"
diff --git a/app/views/comments/_show.html.haml b/app/views/comments/_show.html.haml
new file mode 100644
index 0000000..a52f960
--- /dev/null
+++ b/app/views/comments/_show.html.haml
@@ -0,0 +1,2 @@
+%p= comment.name
+%p= comment.body
diff --git a/app/views/posts/_form.html.haml b/app/views/posts/_form.html.haml
new file mode 100644
index 0000000..746750b
--- /dev/null
+++ b/app/views/posts/_form.html.haml
@@ -0,0 +1,2 @@
+%p= text_field :title
+%p= text_area :body
diff --git a/app/views/posts/edit.html.haml b/app/views/posts/edit.html.haml
index f13ad9b..63aef87 100644
--- a/app/views/posts/edit.html.haml
+++ b/app/views/posts/edit.html.haml
@@ -7,9 +7,10 @@
= error_messages_for @post
= form_for(@post, :action => url(:post, @post)) do
+ = partial :form
%p
= submit "Update"
= link_to 'Show', url(:post, @post)
|
-= link_to 'Back', url(:posts)
\ No newline at end of file
+= link_to 'Back', url(:posts)
diff --git a/app/views/posts/index.html.haml b/app/views/posts/index.html.haml
index da7adb5..b50dc0b 100644
--- a/app/views/posts/index.html.haml
+++ b/app/views/posts/index.html.haml
@@ -1,16 +1,9 @@
-%h1
- Posts controller, index action
-%p
- Edit this file in
- %tt
- app/views/posts/index.html.erb
%table
%tr
- for post in @posts
+ %td= post.title
%tr
- %td
- = link_to 'Show', url(:post, post)
- %td
- = link_to 'Edit', url(:edit_post, post)
+ %td= link_to 'Show', url(:post, post)
+ %td= link_to 'Edit', url(:edit_post, post)
-= link_to 'New', url(:new_post)
\ No newline at end of file
+= link_to 'New', url(:new_post)
diff --git a/app/views/posts/new.html.haml b/app/views/posts/new.html.haml
index 658ab69..6ba3de9 100644
--- a/app/views/posts/new.html.haml
+++ b/app/views/posts/new.html.haml
@@ -7,7 +7,8 @@
= error_messages_for @post
= form_for(@post, :action => url(:posts) ) do
+ = partial :form
%p
= submit "Create"
-= link_to 'Back', url(:posts)
\ No newline at end of file
+= link_to 'Back', url(:posts)
diff --git a/app/views/posts/show.html.haml b/app/views/posts/show.html.haml
index b53540d..d0591f1 100644
--- a/app/views/posts/show.html.haml
+++ b/app/views/posts/show.html.haml
@@ -1,10 +1,12 @@
-%h1
- Posts controller, show action
-%p
- Edit this file in
- %tt
- app/views/posts/show.html.erb
+%p= @post.title
+%p= @post.body
+
+%hr
+%h1 Comments
+%p= partial "comments/show", :with => @post.comments.reverse, :as => :comment
+%hr
+%p= partial("comments/comment")
= link_to 'Edit', url(:edit_post, @post)
|
-= link_to 'Back', url(:posts)
\ No newline at end of file
+= link_to 'Back', url(:posts)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment