File Uploads and Performing Debugging in Rails
Put together the full file uploading video feature into the Ruby on Rails application and get it fully functional.
Guide Tasks
  • Read Tutorial
  • Watch Guide Video
Video locked
This video is viewable to users with a Bottega Bootcamp license

I think this will be an exciting episode as this is where we put together all that we've learned so far. To do that, go to the task model file, which is, task.rb and add the upload functionality with this code:

mount_uploader :task_file, TaskFileUploader

large

Next, start the server and go to your browser. Pick any project and add a task to it and upload a file in the tasks/new page. Upload just about any file to test and click the Create Task button. This should take you to the projects page where all the tasks are listed. Click on the task you just created. On the show page, you should be able to see the "Attachment" link, and when you click this link, you should be able to see the file that you uploaded.

If you right-click on the "Attachment" link, copy the URL and paste it in the browser, you can see that it's a really long URL as this is a cached version of AWS. This is a great way to save your image files as it protects your account.

So, all the functionality is working as far as our tasks and projects are concerned.

Now, I'm going to show you a little bug that you may encounter.

Click on a project that does not have a description, go to its show page and add a new task. When you click on the attachment button and upload a file, you'll see nothing changes.

If you open up the rails console, you can see that it rolled back the transaction right after the Select Count query.

large

This is not a good thing in a real-time application, so it's important to know what's causing it. If you open your project.rb file, you can see the code:

validates_presence_of :title :description :percent_complete

This code prevents the application from updating a project when the description field is empty. In general, this error is something you have to watch out for because you will create the application and add validations only later on. So, there is always a possibility for bugs like this.

Also, if you encounter errors like this, revisit one of the callbacks and see if any logic here is what is causing the problem. Another way is to install a gem called "Pry" that allows you to open a new session in your terminal so you can see the line-by-line execution of the code. When a particular line has not happened the way it should, you know that's where the problem lies.

If you just starting off in Rails, what you can do is insert some content inside your methods to know whether that method was called at all. For example, in this case, I'm going to add some content to the update_percent_complete method in task.rb.

large

If you see in the above screenshot, I've add a couple lines of content at the beginning and at the end of the callback to know if the logic was executed at all.

To check, go back to your browser and try adding a task to the project without a description. Nothing changes in your browser, but if you come back to your console, you can see that the first message Beginning of callback is display, but the message End of callback is not displayed.

large

So, this is an easy way to debug your code.