I'd like to refactor the posts/show.html.erb file as follows.
What I like to do is first at the very top, I'd like to have the code for
displaying a particular post.
Underneath that I'd like to show all of the comments that are associated with
that post.
Beneath that, let's have a form for
adding new comments if you'd like to that particular post.
And then let's put the same links at the bottom, the edit and back links.
The places where I'm going to have to change some things are these.
Do we have the code for displaying a post?
But we don't have yet the code here for displaying comments or a form for
creating a new post.
Now as a part of this refactoring,
I'm going to create something in Rails called a partial.
And what a partial is, is it's a fragment of view code.
And the first one I'm going to do it for is for posts.
So to display a post I'm going to create a div and
this is a helper method for creating a div.
I'm going to hand it the post and we're going to print the post title, and
this is within embedded Ruby as an h2, second level heading here.
And this h method here, what that does is it escapes HTML.
So if you have some funny things in the title It'll still be able to escape it,
produce the appropriate HTML to allow it to display in your browser,
the next line displays the body of the post, so again, we're first escaping any
unsafe HTML and then simple format will produce a very simple HTML formatting.
Store all of this under views > posts in the file_post.html.arb.
Very important,
partials have to have an underscore as the first character in their file name.
The whole goal with partials is to refactor your code
to create these snippets that can be used over and over again.
Let me show you how.
Going back to our show.html.arb, I can remove all of this code, this
was the code for showing the title and the body, and instead just call that partial.
And here's how it's done, just render post.
And post, it knows to look for _post.html.arb In the post sub directory.
Now let's create a partial for adding comments below the posts.
So go to your views comments subdirectory, and
create a file called _comment.html.erb.
And let's put this in it.
We're going to create a div for
our comments and here's just a little HTML formatting.
The first says that this is a paragraph.
That's what the p is.
And then I like it strong so
we want this emphasized in however emphasize is being styled.
And then we're going to put posted and
we're going to use this helper method time_ago_in_words.
So this will tell us how long ago this particular comment was created.
Now notice that I'm not using an @ symbol here.
That's because I'm going to be dealing with a whole list of comments.
So this div is going to need to work for each of those comments.
Let's go ahead and use this partial now in our post show method.
So we'll place it right here and what I'm going to do is I'm going to create a div
for all of the comments, I'm going to give a div id for all of those comments.
And here is the partial,
I'm calling that partial on each of the individual comments.
So this goes post.comments, and then I'm using reverse because I want to display
these In reverse chronological order.
The most recent comment should appear on top and the oldest at the bottom.
Let's go ahead and save this, start up or server, and
see if everything's still working.
I've started the server, and
if I go to local host 3000, we see there's the rails app.
Let's see if I can still view my posts, and there they are.
And if I take a look at a particular post, and we see that everything is working.
Here's the title of the post, the body, and then all of the comments in reverse
chronological order, and finally the links at the bottom of the page.
Here's the code for the form.
So we're going to use the form_for method and just copy some of this, we'll explain
more of it later when we have an opportunity to get deeper into HTML.
But I'm going to pass a post and then we're going to create a new comment.
And that's going to be the basis for how we fill in this form.
This remote true, this is how we're going to end up calling JavaScript.
And then there's going to be a label called new comment that will
appear in the browser.
And then this is a control.
This text area is we're going to be able to enter our text for the new comment.
And then there's a Submit button that will appear at the bottom.
Let's save this and start up the server and see if this now works.
I'm just going to hit Refresh and see what happens.
And we'll see an error was generated.
There's a problem here.
There's no path, there's no route for post comments path.
And I'll show you the reason for this and how to fix it.
To show you what the issue is, go to the root of your Rails application, and
type rake routes.