Create a new page in Rails ::::::::::::::::::::::::::::::::::::::::::: https://melvinchng.github.io/rails/CreateARailsApplication.html#29-create-a-new-page
2.9 Create a New Page
A page can be created under any controller. However, the page method in the controller has the match the filename of your view. To create a new page in Post controller, navigate to the directory below and add the code in Table 2.9.1 to controller. Figure 2.9.1 shows the result after the code is added.
Table 2.9.1: Code to add to controller
#MyApp/controllers/posts_controller.rb
def more
end

Figure 2.9.1: Result of the code added
Create a View file with filename and path listed below as shown in Table 2.7.2. Add Hello World! to the file that you create. Figure 2.9.2 shows the result of the operation in Table 2.9.2.
Table 2.9.2: Create a view file and insert Hello World!
#MyApp/views/posts/more.html.erb
Hello World!

Figure 2.9.2: Result of the operation shows in Table 2.9.2
The last step would define and tell your web server where to find and retrieve the file. Add the following code to the file shown in Table 2.9.3. Figure 2.9.3 screenshot with the operations carried out is shown below. more_info is the route that you will need to enter in your web browser to view the content of the page while posts is the folder in the directory MyApp/views and more is the of the file where you want your Web Server to fetch.
Table 2.9.3: Setting up the routes.rb
#MyApp/config/routes.rb
get 'more_info', to: 'posts#more'

Figure 2.9.3: The code, as shown in line 6, is added to routes.rb
To view the page that you have created, insert the address below in your Web Browser. Figure 2.9.3 shows the Hello World! is rendered.
localhost:3000/more_info
Figure 2.9.4: Hello World! is shown in the page
If you are unable to access this page, check your route and local address to your Web Application.
Comments
Post a Comment