Skip to content
This repository has been archived by the owner on Jul 13, 2023. It is now read-only.

Commit

Permalink
Clarify Rails 3/4 things in the README
Browse files Browse the repository at this point in the history
  • Loading branch information
Jon Yurek committed Nov 8, 2013
1 parent 8e6c19a commit efdf952
Showing 1 changed file with 26 additions and 8 deletions.
34 changes: 26 additions & 8 deletions README.md
Expand Up @@ -96,17 +96,26 @@ end
Quick Start
-----------

In your model:
### Models

**Rails 3**

```ruby
class User < ActiveRecord::Base
attr_accessible :avatar
has_attached_file :avatar, :styles => { :medium => "300x300>", :thumb => "100x100>" }, :default_url => "/images/:style/missing.png"
end
```
(Note: if you are using Rails 4, leave attr_accessible out to accommodate [strong parameters](https://github.com/rails/strong_parameters). Instead add the parameters in the controller (see below).)

In your migrations:
**Rails 4**

```ruby
class User < ActiveRecord::Base
has_attached_file :avatar, :styles => { :medium => "300x300>", :thumb => "100x100>" }, :default_url => "/images/:style/missing.png"
end
```

### Migrations

```ruby
class AddAvatarColumnsToUsers < ActiveRecord::Migration
Expand All @@ -122,24 +131,31 @@ end

(Or you can use migration generator: `rails generate paperclip user avatar`)

In your edit and new views:
### Edit and New Views

```erb
<%= form_for @user, :url => users_path, :html => { :multipart => true } do |form| %>
<%= form.file_field :avatar %>
<% end %>
```

In your controller:
### Controller

**Rails 3**

```ruby
def create
@user = User.create( params[:user] )
end
```
If you are using Rails 4, add this to your controller as well:

**Rails 4**

```ruby
def create
@user = User.create( user_params )
end

private

# Use strong_parameters for attribute whitelisting
Expand All @@ -150,15 +166,17 @@ def user_params
end
```

In your show view:
### Show View

```erb
<%= image_tag @user.avatar.url %>
<%= image_tag @user.avatar.url(:medium) %>
<%= image_tag @user.avatar.url(:thumb) %>
```

To detach a file, simply set the attribute to `nil`:
### Deleting an Attachment

Set the attribute to `nil` and save.

```ruby
@user.avatar = nil
Expand Down

0 comments on commit efdf952

Please sign in to comment.