Verify file type in Ruby

Posted: February 27th, 2010 | Author: admin | Filed under: Uncategorized | Tags: , , , , , , | No Comments »

I was unable to find anything quite like this in Google, so I had to make one.  When validating a file type, you don’t want to resort to telling your user a file is the wrong format when they think is.  If for some reason a user’s file doesn’t contain an extension, you should check the mime type as a last resort.  Linux and Mac users may often not use file extensions.  Note that if you are doing this for security reasons, you should check both the extension and mime-type instead of one or the other.

Here was my solution for checking to see if a file was a document for a Recruiting web site.  Note that you will need MIME::Types which is included by default in Ruby on Rails.

def is_document?(file)
  extensions = %w( pages pdf doc docx rtf txt odt )
  # used http://filext.com/ for mime types
  mimetypes = %w( application/pdf application/x-pdf application/acrobat applications/vnd.pdf text/pdf text/x-pdf
                  application/doc appl/text application/vnd.msword application/vnd.ms-word application/winword application/word application/x-msw6 application/x-msword
                  application/vnd.openxmlformats-officedocument.wordprocessingml.document
                  application/rtf application/x-rtf text/rtf text/richtext application/msword application/x-soffice
                  text/plain application/txt browser/internal text/anytext widetext/plain widetext/paragraph
                  application/vnd.oasis.opendocument.text application/x-vnd.oasis.opendocument.text
                  application/x-iwork-pages-sffpages )
  if file.include?('.')
    return extensions.include? file[ file.rindex('.')+1, file.length ]
  else
    return mimetypes.include? MIME::Types.of( file ).to_s
  end
end

View All of Your Database Tables in Rails

Posted: May 18th, 2009 | Author: admin | Filed under: Rails | Tags: , , , | No Comments »

When working on a Ruby on Rails project, you often find yourself needing to see what the names of the columns in your tables are.  This can be done by simply looking at your migration files, but after several migrations in, this can be a confusing headache.  This is my very simple solution to seeing all of the tables listed on one page in my project.

1. Create a New View

I called mine models.html.erb.  Paste in the folling code:

<% @models.each do |model| %>
  <h3 style="margin:0;padding:0"><%=h model %></h3>
  <table width="400">
  <% model.constantize.columns.each do |col| %>
    <tr>
      <td><b><%=h col.name %></b></td><td><%=h col.sql_type %></td><td><%=h col.default.to_s %></td>
    </tr>
  <% end %>
  </table>
  <br />
<% end %>

2. Add Your Models in the Controller

Go to the controller that is part of your view and add the corresponding method.  In the method you just need to add a line telling it the names of all of your models. For example:

@models = ['User','Article','Subject']

3. Take a Look!

The resulting table will have 3 columns: the name of the column in the database, the data type, and the default value (if one exists).
viewexample