= Watarase Watarase is a rails plugin for Image Upload. Images are stored into the database as binary data. This plugin is inteded to be handled as a small image of the icon. == Installation Add to Gemfile following gem 'watarase' gem 'rmagick' Now, watarase plugin setting following gem 'watarase', :git => 'git://github.com/kobayashi-tbn/watarase.git' == Getting Started Any model of the target is present is expected. example rails generate scaffold user username:string email:string That model is the image handler. Generate image holder. This is a model that is made separately. rails generate watarase:uploader user The generator made UserImageHolder the model class and the migration file. Example of migration file is following. class CreateUserImageHolders < ActiveRecord::Migration def change create_table :user_image_holders do |t| t.string :user_username t.string :filename t.string :content_type t.binary :image_data t.binary :image_thumb t.timestamps end end end In this case, the foreign key 'user_username' is type :string. It was named {model name}_{pk} automatically. == Active Record Define 'acts_as_image_hander' into Image handler model (ex. User). class User self.primary_key :username # if this model's PK was 'username'. acts_as_image_handler # define image hander end == Routing Add 'load_image' action in 'config/routes.rb' resources :users do member do get 'load_image' end end == Controller Define 'image_loadable ' definition in controllers. class UsersController < ApplicationController image_loadable :user : Set image holder to image handler by method 'set_iamge_holder'. def create @user = User.new(user_params) set_image_holder @user : def update set_image_holder @user : == View Examples _form.html.erb for User <%= form_for(@user) do |f| %> <% if @user.errors.any? %>

<%= pluralize(@user.errors.count, "error") %> prohibited this user from being saved:

<% end %>
<%= f.label :username %>
<%= f.text_field :username %>
<%= f.label :email %>
<%= f.text_field :email %>
<%= f.label :image_file %>
<%= f.file_field :image_file %>
<% unless @user.new_record? %>
<%= f.check_box :remove_image %> <%= f.label :remove_image %>
<% end %>
<%= f.submit %>
<% end %> Add file_field form, and check box for remove the image. Parameter name is fixed, :image_file and :remove_image. Examples image load in index.html.erb <% @users.each do |user| %> <%= image_tag image_thumb_path user if user.user_image_holder %> <%= user.username %> <%= link_to 'Show', user %> <%= link_to 'Edit', edit_user_path(user) %> <%= link_to 'Destroy', user, method: :delete, data: { confirm: 'Are you sure?' } %> <% end %> == TODO * Configuring parameters for plugin settings. * Secure upload by file extentions. This project rocks and uses MIT-LICENSE.