Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ## post/show (posts_controller.rb) [ruby_on_rails]
- def show
- @post = Post.find(params[:id])
- @comments = @post.comments
- respond_to do |format|
- format.html # show.html.erb
- format.xml { render :xml => @post }
- end
- end
- ## comment/create (comments_controller.rb) [ruby_on_rails]
- def create
- @post = Post.find(params[:post_id])
- @comment = @post.comments.build(params[:comment])
- if @comment.save
- redirect_to @post
- else
- # need to create an empty views/comments directory so it can go back a level into the posts
- render :action => "../posts/show"
- end
- end
- ## form_for @comment (posts/show.html.erb) [html_rails]
- <% form_for @post.comments.build, :url => post_comments_path(@post) do |f| %>
- <p><%= f.error_messages %></p>
- <p><%= f.text_area :body %></p>
- <p><%= f.submit "Add comment" %></p>
- <% end %>
- ## list current comments (posts/show.html.erb) [html_rails]
- <% @comments.each do |comment| %>
- <p>
- <b>Posted <%= time_ago_in_words(comment.created_at) %> ago</b><br/>
- <%= comment.body %>
- </p>
- <% end %>
Add Comment
Please, Sign In to add comment