Episode completed

Next epsiode starting in 3 seconds

Published at


Now that we can write posts in our blog, we can start making it look nice. To do that, let’s open the Show View for Posts in our code editor. This file defines what it should look like if you open a post. As you can see here, the ShowView gets a post. We can now use this post-object to get information out of it. For example, I want the title of the post to be the headline of the page. Let’s delete the placeholder text and add a bit of code. Code is defined in brackets like this. This way, code and plain text can be differentiated. To get the title of the post, we can use “get title post”. We can do the same thing here to display the body of the post. If we save this and go back to the browser we can see our changes. If we now go back to the list of posts, also called index view, we can change things here as well. Let’s only display the title of the post. Now we can make the title clickable and remove the buttons by wrapping the title in an a tag and setting the hypertext reference to the ShowPostAction. To see all the possible actions and what they do, let’s go into the Posts Controller. Here you can see all the actions and how you can access them. Some actions require the ID of the post, to know exactly which post to use. Since we are already here, let’s add a validation for the post creation. Since a post without a title can’t be accessed, we need to make sure that you can’t create a blog post without a title. To do that, we can add a validateField statement to the build function at the bottom of our controller. This function is used to get the information you just put in when creating or editing a post. Going back to the index view, we can see that our post is a clickable title now. If we now create a post without a title, we get a neat error message.