7. Create Controllers
September 2015 · 2 minute read
Now that I have successfully created a model and migrated a database for income, it’s time to create controllers and views to be able to access the income information visually.
The following terminal command will generate a new controller called “income_controller.rb” under app/controllers
$ bin/rails generate controller Income
As you can see, I have created an income controller which currently does nothing
If you look under app/views, another new income folder has also been created. In here is where we will store all view files relating to “income.” View files need to be compatible with web browsers, ie html files. I am going to create will be called index.html.erb
For the time being, I simply want to ensure I can access the page so I have simply typed the word “Income” in our new index file
Going back to my income controller, I now need to ensure I can access my new view
The last step to ensure that my new controller and view are working properly is to ensure they have been correctly routed in app/config/routes.rb - I want to be able to access (get) the new view I have just created
After changing the routes files, I need to remigrate again with the following command
$ rake db:drop && rake db:create && rake db:migrate
After running the rails server again, I can now navigate to localhost:3000/income/index and we can see that my new view has worked
Keep watching to see how I use forms and bootstrap modals to update income information.