Ok after completing part 2 we can now see each article rendered as it was added, however, we now need a way to see a list of articles. The first thing I have done is added a new method to the index articles controller, this method retrieves all of our articles using Article.all.
As we did before; we now create the HTML for index and use the values from @articles to list them.
Here we create a loop using Ruby’s each do loop to render each article in the page, each time we pass through this loop we take one of the articles from the database. As we do this we include the article title, text, and a link to the article itself, this is done by passing it back to the show method we previously made.
Here is the result when visiting our localhost/articles…
Now to add some links so we can navigate around the application, Rails conveniently has a helper method called link_to. I added a link for the new article page in the articles index, a link to the articles list in the welcome index, a return link on the create a new article page, and a return link on each individual article.
<%= link_to ‘New article’, new_article_path %>
<%= link_to ‘My Blog’, controller: ‘articles’ %>
<%= link_to ‘Back’, articles_path %>
<%= link_to ‘Back’, articles_path %>
The link_to helper method as you can see takes two arguments, the first being the name of the link, the second being where we are linking too. If you are wondering why the second link takes a different argument ‘controller: articles’ it is because the other actions are situated in the article controller. By default Rails will select the controller you are situated in, however, if you want to link an action to another you will need to specify the controller you want to use.
Validation lets add some! Take a look at the code I added in the Article model.
The Article class inherits from ApplicationRecord and the ApplicationRecord inherits from ActiveRecord::Base, meaning we inherit many useful helper methods from this parent class. In the code above I’m using the validates helper method in order to validate the presence of a title is true, with a minimum length of 5 characters.
Now we have this validation we will receive a true or false boolean returned informing us if it passed our conditions upon saving it to our database. Due to this we now need to instruct Rails what to do in the event false is returned, in the code below I have resolved this issue in the articles controller.
Here we are saying in the event a new article is not successfully added to our database rerender the current page which is ‘new’, next we will write some code to show an error in the event this happens. If the article is however successfully saved we will automatically show it rendered on the page with redirect_to @article.
So let's get an error message to show, take a look at the code added in the article/new HTML form_with method.
Let's break this code down, we contain all of this code inside one div within our form. In this code we use @article.errors.any? to determine if an error occurred, if so we will display the error message. In the message itself we use pluralize this is another helper method that takes 2 arguments, a number, and a string. When the number or in our case the number of errors is greater than one the string will be pluralized. Following this, we access the error messages from the article object and render each in a list. In Rails fields that contain errors are automatically wrapped in class field_with_errors, you can keep this in mind when it comes to styling them.
We should now have an error message show the following when we try to save an invalid article.
Continue to part 4.