Created
September 30, 2014 03:11
-
-
Save jacoyutorius/6144078a172f3440ea7c to your computer and use it in GitHub Desktop.
【Rails】Paperclipで保存した画像のサムネイルの取得方法 ref: http://qiita.com/jacoyutorius/items/6844e72ece23dcac8560
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class CreateUsers < ActiveRecord::Migration | |
| def change | |
| create_table :users do |t| | |
| t.integer :name | |
| t.attachment :icon | |
| end | |
| end | |
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class User < ActiveRecord::Base | |
| [:icon].each do |col| | |
| has_attached_file col , :styles => {:medium => "300x300", :thumb => "100x100"}, | |
| :default_url => "./images/:style/missing.png", | |
| :url => "/images/:class/:attachment/:id_partition/:style/:filename" | |
| validates_attachment_content_type col, | |
| :content_type => ["image/png", "image/jpg", "image/jpeg"] | |
| end | |
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| irb(main):003:0> User.first.icon(:thumb) | |
| User Load (0.4ms) SELECT `users`.* FROM `users` ORDER BY `users`.`id` ASC LIMIT 1 | |
| => "/images/users/icons/000/000/001/thumb/picture?1412045787" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <div> | |
| <%= image_tag(@user.icon(:thumb)) %> | |
| </div> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment