If you submit the form again now, Rails will complain about not finding the show action. That's not very useful though, so let's add the show action before proceeding.
As we have seen in the output of bin/rails routes, the route for show action is as follows:
article GET /articles/:id(.:format) articles#show
The special syntax :id tells rails that this route expects an :id parameter, which in our case will be the id of the article.
As we did before, we need to add the show action in app/controllers/articles_controller.rb and its respective view.
A frequent practice is to place the standard CRUD actions in each controller in the following order: index,show,new,edit,create,updateanddestroy. You may use any order you choose, but keep in mind that these are public methods; as mentioned earlier in this guide, they must be placed before declaring private visibility in the controller.
Given that, let's add the show action, as follows:
class ArticlesController < ApplicationControllerdef show@article = Article.find(params[:id])enddef newend# snippet for brevity
A couple of things to note. We use Article.find to find the article we're interested in, passing in params[:id] to get the :id parameter from the request. We also use an instance variable (prefixed with @) to hold a reference to the article object. We do this because Rails will pass all instance variables to the view.
Now, create a new file app/views/articles/show.html.erb with the following content:
<p><strong>Title:</strong><%= @article.title %></p><p><strong>Text:</strong><%= @article.text %></p>
With this change, you should finally be able to create new articles. Visit http://localhost:3000/articles/new and give it a try!
We still need a way to list all our articles, so let's do that. The route for this as per output of bin/rails routes is:
articles GET /articles(.:format) articles#index
Add the corresponding index action for that route inside the ArticlesController in the app/controllers/articles_controller.rb file. When we write an index action, the usual practice is to place it as the first method in the controller. Let's do it:
class ArticlesController < ApplicationControllerdef index@articles = Article.allenddef show@article = Article.find(params[:id])enddef newend# snippet for brevity
And then finally, add the view for this action, located at app/views/articles/index.html.erb:
<h1>Listing articles</h1><table><tr><th>Title</th><th>Text</th><th></th></tr><% @articles.each do |article| %><tr><td><%= article.title %></td><td><%= article.text %></td><td><%= link_to 'Show', article_path(article) %></td></tr><% end %></table>
Now if you go to http://localhost:3000/articles you will see a list of all the articles that you have created.
You can now create, show, and list articles. Now let's add some links to navigate through pages.
Open app/views/welcome/index.html.erb and modify it as follows:
<h1>Hello, Rails!</h1><%= link_to 'My Blog', controller: 'articles' %>
The link_to method is one of Rails' built-in view helpers. It creates a hyperlink based on text to display and where to go - in this case, to the path for articles.
Let's add links to the other views as well, starting with adding this "New Article" link to app/views/articles/index.html.erb, placing it above the tag:
<%= link_to 'New article', new_article_path %>
This link will allow you to bring up the form that lets you create a new article.
Now, add another link in app/views/articles/new.html.erb, underneath the form, to go back to the index action:
<%= form_with scope: :article, url: articles_path, local: true do |form| %>...<% end %><%= link_to 'Back', articles_path %>
Finally, add a link to the app/views/articles/show.html.erb template to go back to the index action as well, so that people who are viewing a single article can go back and view the whole list again:
<p><strong>Title:</strong><%= @article.title %></p><p><strong>Text:</strong><%= @article.text %></p><%= link_to 'Back', articles_path %>
If you want to link to an action in the same controller, you don't need to specify the :controller option, as Rails will use the current controller by default.
In development mode (which is what you're working in by default), Rails reloads your application with every browser request, so there's no need to stop and restart the web server when a change is made.