Commit efdf952e by Jon Yurek

Clarify Rails 3/4 things in the README

parent 8e6c19a9
...@@ -96,7 +96,9 @@ end ...@@ -96,7 +96,9 @@ end
Quick Start Quick Start
----------- -----------
In your model: ### Models
**Rails 3**
```ruby ```ruby
class User < ActiveRecord::Base class User < ActiveRecord::Base
...@@ -104,9 +106,16 @@ class User < ActiveRecord::Base ...@@ -104,9 +106,16 @@ class User < ActiveRecord::Base
has_attached_file :avatar, :styles => { :medium => "300x300>", :thumb => "100x100>" }, :default_url => "/images/:style/missing.png" has_attached_file :avatar, :styles => { :medium => "300x300>", :thumb => "100x100>" }, :default_url => "/images/:style/missing.png"
end 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 ```ruby
class AddAvatarColumnsToUsers < ActiveRecord::Migration class AddAvatarColumnsToUsers < ActiveRecord::Migration
...@@ -122,7 +131,7 @@ end ...@@ -122,7 +131,7 @@ end
(Or you can use migration generator: `rails generate paperclip user avatar`) (Or you can use migration generator: `rails generate paperclip user avatar`)
In your edit and new views: ### Edit and New Views
```erb ```erb
<%= form_for @user, :url => users_path, :html => { :multipart => true } do |form| %> <%= form_for @user, :url => users_path, :html => { :multipart => true } do |form| %>
...@@ -130,16 +139,23 @@ In your edit and new views: ...@@ -130,16 +139,23 @@ In your edit and new views:
<% end %> <% end %>
``` ```
In your controller: ### Controller
**Rails 3**
```ruby ```ruby
def create def create
@user = User.create( params[:user] ) @user = User.create( params[:user] )
end end
``` ```
If you are using Rails 4, add this to your controller as well:
**Rails 4**
```ruby ```ruby
def create
@user = User.create( user_params )
end
private private
# Use strong_parameters for attribute whitelisting # Use strong_parameters for attribute whitelisting
...@@ -150,7 +166,7 @@ def user_params ...@@ -150,7 +166,7 @@ def user_params
end end
``` ```
In your show view: ### Show View
```erb ```erb
<%= image_tag @user.avatar.url %> <%= image_tag @user.avatar.url %>
...@@ -158,7 +174,9 @@ In your show view: ...@@ -158,7 +174,9 @@ In your show view:
<%= image_tag @user.avatar.url(:thumb) %> <%= 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 ```ruby
@user.avatar = nil @user.avatar = nil
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment