Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save mikeymicrophone/1655040 to your computer and use it in GitHub Desktop.
Save mikeymicrophone/1655040 to your computer and use it in GitHub Desktop.
using carrierwave (for uploading files)
class UsersHaveAvatar < ActiveRecord::Migration
def up
add_column :users, :avatar, :string
end
def down
remove_column :users, :avatar
end
end
<%= form_for @user do |f| %>
<%= f.label :avatar %><br />
<%= f.file_field :avatar %>
<%= f.submit %>
<% end %>
Carrierwave saves files and stores their names so that it can find them later. It stores the name in the database.
It can save the files themselves in a few different places. Usually you either store the file on the same computer as the server runs, or use a service (like Amazon S3) to store them on another server somewhere on the web.
The uploader keeps track of which storage method is in use, so carrierwave knows where to look, and it uses the filename stored in the database to determine which file to grab. It can then generate a url that allows the end user's browser to load the file.
You can also allow users to download files (as opposed to displaying them in HTML). The send_file method is used for this - you would call this method in a controller, not a view.
https://github.com/jnicklas/carrierwave is the best place (that I know of) to get information about using carrierwave, which is a gem that facilitates uploading and saving files.
Here are the basic steps for configuring it.
First, add this line to your Gemfile
gem 'carrierwave'
To install the gem, either use
bundle
or
gem install carrierwave
Now you can use a generator to generate an uploader class.
rails generate uploader Avatar
You can replace Avatar with the name of your uploader.
The next step is to add a string field to the table you will be storing files for. So, if you are storing an avatar for a user, you would add a column called 'avatar' to the table 'users'.
Generate a migration
rails g migration users_have_avatar
Then find the migration file it generates (in your db/migrations folder). There are two empty methods here, called up and down. Edit them so the file looks like the file below.
Then run your migration.
rake db:migrate
Now your database is ready to store the information that carrierwave needs. The next step is telling the user model (or whatever model you are storing uploads for) that carrierwave will be managing those files for it.
In the file user.rb (again, adjust this based on what model you are actually using), add this line.
mount_uploader :avatar, AvatarUploader
Note that these two arguments don't actually have to match each other. The first one has to match the column you added to the db, and the second one has to match the name of the uploader you generated, but they do not both have to be the same word (in this case, avatar and Avatar). Usually they are though, for simplicity.
Now you can use the method avatar to refer to the file that is attached to a given record.
In a form, you can enable users to upload files like in the below template.
Let's assume for now that you're using it for image files. You can use avatar.url to display the image as in the other template.
<% @user = User.last %>
<%= image_tag @user.avatar.url %>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment