How to Use the Rails Controller Generator
In this lesson, we are going to begin the process of integrating our uploader with our Task model so that we can upload files from a form in the application to the CDN.
Guide Tasks
  • Read Tutorial
  • Watch Guide Video
Video locked
This video is viewable to users with a Bottega Bootcamp license

In this lesson, we are going to begin the process of integrating our uploader with our Task model so that we can upload files from a form in the application to the CDN.

As a first step, I'm going to add a field called task_file to the tasks table. To do this, go to your console and type the command:

rails g migration add_task_file_to_tasks task_file:text

If you notice, I'm setting a text data type for the name of my task_file instead of a string because AWS can sometimes return a long file name, especially if it is bringing it back from its cache.

Next, run the rake db:migrate command to update the database.

It's a good idea to go to your schema file to check if task_file is added to the tasks table.

large

Next, we are going to create our controller with a generator instead of using a scaffold. To do that, go to your console and type:

rails g controller tasks show new edit

A rule of thumb when it comes to naming your files is to have it singular for your model and plural for your controller, and this is why my controller file is called tasks. This controller will have three methods called show, new and edit.

large

If you notice, this command has created routes and views for each of these methods. This is also the reason why I did not add methods like update and delete, because these methods don't really need views.

Now, if you go to tasks_controller.rb, you can see that it has created a class called TasksController that inherits from ApplicationController. You can also see the three methods, though they are empty for now.

large

We'll talk about customization in the next lesson.