Skip to content

Instantly share code, notes, and snippets.

@jacoyutorius
Created September 30, 2014 03:11
Show Gist options
  • Select an option

  • Save jacoyutorius/6144078a172f3440ea7c to your computer and use it in GitHub Desktop.

Select an option

Save jacoyutorius/6144078a172f3440ea7c to your computer and use it in GitHub Desktop.
【Rails】Paperclipで保存した画像のサムネイルの取得方法 ref: http://qiita.com/jacoyutorius/items/6844e72ece23dcac8560
class CreateUsers < ActiveRecord::Migration
def change
create_table :users do |t|
t.integer :name
t.attachment :icon
end
end
end
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
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"
<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