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. Twitter Bootstrap for UI etc.
- 2. Devise for authentication
- 3. Paperclip for handling uploads
- 4.Annotate for detailed model info
Listing All Users Registered Using Devise
1 |
rails generate controller user index show |
Add the following to your users_controller to get all the users.
1 2 3 |
def index @users = User.all end |
Now ensure that your route.rb files has the following:
1 2 |
devise_for :users resources :users |
You can always check the avialable routes in your RoR app by running
1 |
rake routes |
Now edit the “users/index.html.erb” to display the users currently registered
All Users
1 |
<% @users.each do |user| %> |
- <%= link_to user.email, user %>
<% if current_user.admin? %>
| <%= link_to “Delete”,user, :confirm => “Are you sure?”, :method => :delete %><% end %>
1 |
<% 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:
1 2 3 4 5 |
class User < ActiveRecord::Base has_many :uploads,:foreign_key => 'user_id',:dependent => :destroy class Upload < ActiveRecord::Base belongs_to :user |
If for example however you do not use those methods created by the relationship such as
1 |
user.upload |
to upload a book, but instead use
1 |
Upload.new |
, the user_id in the uploads table will not be populated with the id of the corresponding user. This is because
1 |
Uploads.new |
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