Ruby on Rails Note To Self – File Upload App

Wow, this article has been long overdue. I have been learning so much, but have not given this blog the priority it deserves. I refuse to use the excuse of “not enough time” 🙁

Even though , the full code is in my GitHub. This post is a reminder and a compilation of the resources , I used to complete my first webapp using Ruby on Rails.

The File Upload App currently allows a user to upload and save any file type, download the files belonging to that user and of course deleting the uploaded files if necessary. It also has a small admin mode, where the user with administrator privileges can see all files uploaded by all users and has the ability to delete both users and files uploaded.

I used the following to develop the complete WebApp

  1. 1. Twitter Bootstrap for UI etc.
  2. 2. Devise for authentication
  3. 3. Paperclip for handling uploads
  4. 4.Annotate for detailed model info

 

Listing All Users Registered Using Devise

Add the following to your users_controller to get all the users.

Now ensure that your route.rb files has the following:

You can always check the avialable routes in your RoR app by running

Now edit the “users/index.html.erb” to display the users currently registered

 

All Users

  • <%= link_to user.email, user %>
    <% if current_user.admin? %>
    | <%= link_to “Delete”,user, :confirm => “Are you sure?”, :method => :delete %><% end %>


ActiveRecord

When a relationship is created using active record. For example in this case a has_many and belongs_to relationship between uploads and users, certain methods are created. A user has_many uploads and a upload belongs_to a user. In this case the following methods were created by using the above relationship:

If for example however you do not use those methods created by the relationship such as

to upload a book, but instead use

, the user_id in the uploads table will not be populated with the id of the corresponding user. This is because

has no association with the current user and as a result the user_id is left nill everytime you upload files. Note to self, double check methods called , no foreign key or the “magic” of rails will help here.

Routes

The controller and its action assigned to that specific route is configured in routes.rb. You can specify GET, DELETE,POST or PUT as methods from your view in rails, this is not to be confused with the methods you have available in your controllers. GET, DELETE,POST and PUT should be seen as actions invoked by the user over HTTP that matches back to methods in the controllers to prevent confusion.

I learned a lot more stuff, will try to blog more frequently about my next Webapp project as I progress rather than trying to brain dump after I am done.
Github: https://github.com/skillachie/File-Upload-App

Links

http://ruby.railstutorial.org/ruby-on-rails-tutorial-book
http://railscasts.com/episodes/209-introducing-devise

Leave a Reply

Your email address will not be published. Required fields are marked *