ラベル plugin の投稿を表示しています。 すべての投稿を表示
ラベル plugin の投稿を表示しています。 すべての投稿を表示

2010/03/01

Rails でSSLを使用する

英語のチュートリアルですが、かなりグッド。
ウィンドウズのローカルマシーンで SslRequirement プラグインをインストール
ruby script/plugin install ssl_requirement

application_controller.rb
include SslRequirement

# Below to disable ssl unless system is production
# 下は、本番環境のみ ssl を使う場合。
protected
def ssl_required? 
  ((self.class.read_inheritable_attribute(:ssl_required_actions) || []).include?(action_name.to_sym)) && RAILS_ENV == 'production' 
end  
コントローラーで、ssl を使うアクションを指定
ssl_required  :index, :show, :edit

正しく指定していないページにSSLでアクセスしようとすると次のようなエラーが。。。
Redirected to http://cafetalk.com/en/user/reqs/confirm_req
Filter chain halted as [:ensure_proper_protocol] rendered_or_redirected.
こちらにベターなバージョンあり。 /2010/04/namespace-applicationcontroller.html
上の例だと、application_controller で ssl_required :all ができない

2010/02/18

calendar_date_select plugin doesn't accept minute intervals

calendar_date_select で、分のインターバルを00、30等に設定していても、最初に設定されるデフォルト分数が現在時刻の分の値をとってきてしまう問題の解消法
if (isNaN(this.date)) this.date = new Date();
を、以下に変更
if (isNaN(this.date)) {
   this.date = new Date();
   this.date.setMinutes(Math.ceil(this.date.getMinutes() / this.options.get("minute_interval")) * this.options.get("minute_interval"));
}

2010/01/04

Paperclip と Amazon S3 でファイルを保存する

オンラインショップのサーバー要領がちょっとキツクなったので、画像データを Amazon S3 を使って保存してみょーと思い立つ。
ざっくり説明しますと、Amazon S3が外部ストーレージサービス。データをやり取りした分だけ課金されます。1GBで15円ほどなので、安くは無いけど、いろんなメリットを考えると高くもない。とりあえずテストしてみることに決定~。

まず、Amazon S3 に登録

S3 のデータを見るには『S3Fox』が便利

Firefox のプラグイン『S3Fox』を使うと、FTPクライアント風にAmazon S3の中がみれるよ。

Paperclip 設定の変更

以前、Paperclip の使い方についてメモりましたが、そこからちょいと手を加えます。 environment.rb に aws gem を追加
config.gem "aws-s3", :lib => "aws/s3"
(require する際に require = "aws/s3" とする場合は上のように、 :lib => "aws/s3" を加えて記述) この後、rake gems:install を実行するのを忘れずに。 config/s3.yml を作成
access_key_id: ABCD...
secret_access_key: xYz...
キーとシークレットはここから。 モデルの変更
 #通常バージョン
 has_attached_file :image,
   :styles => {:s => "150x150>", :m =>"320x320>", :l => "500x500>"},
   :url  => "/assets/products/:id/p_:style.:basename.:extension",
   :path => ":rails_root/public/assets/products/:id/p_:style.:basename.:extension"

 #↓S3 バージョン
 has_attached_file :image,
   :styles => {:s => "150x150>", :m =>"320x320>", :l => "500x500>"},
   :storage => :s3,
   :s3_credentials => "#{RAILS_ROOT}/config/s3.yml",
   :path => "products/:id/:style.:extension",
   :url  => ":s3_domain_url",
   :bucket => S3_BUCKET #S3_BUCKET は production/environment.rb development/environment.rb で定義

:s3_domain_url だと http://mybucket.s3.amazonaws.com/products/16152/l.jpg のようになる。
因みに、上で指定した bucket をあらかじめ作っておく必要があるのですー↓ この場合、S3Fox で作成。 これで準備はOK。さっそくアップロードしてみると。。。 入ってる!感激~

Installing plugins from GitHub

ruby script/plugin install git://github.com/thoughtbot/paperclip.git
Somehow I couldn't get this to work with active_scaffold (which I don't use now, anyway). It tells me
Plugin not found: ["git://github.com/activescaffold/active_scaffold.git"]
I have gitbash installed on my Windows so I was finally able to do this.
cd vendor/plugins
git clone git://github.com/activescaffold/active_scaffold.git -o rails-2.3

Paperclip で画像アップロード

ファイルや画像を簡単にアップロードできるプラグイン Paperclip の紹介。
http://github.com/thoughtbot/paperclip/tree/master

最初からテーブルに含める場合

      # migration ファイルに以下を追加
      t.string    :image_file_name, :image_content_type
      t.integer   :image_file_size
      t.datetime  :image_updated_at
      t.string    :image_remote_url
最後の image_remote_url は基本的には必要ないが、URLから画像アップロードしたいので、今回は追加しておく。

既存のテーブルに追加する場合

class AddImageColumnsToProducts < ActiveRecord::Migration
    def self.up
      add_column :products, :image_file_name,    :string
      add_column :products, :image_content_type, :string
      add_column :products, :image_file_size,    :integer
      add_column :products, :image_updated_at,   :datetime
    end

    def self.down
      remove_column :products, :image_file_name
      remove_column :products, :image_content_type
      remove_column :products, :image_file_size
      remove_column :products, :image_updated_at
    end
end

モデルの設定

  has_attached_file :image, 
    :styles => {:s => "150x150>", :m =>"320x320>", :l => "500x500>"},
    :url  => "/assets/products/:id/p_:style.:basename.:extension",
    :path => ":rails_root/public/assets/products/:id/p_:style.:basename.:extension"  
URLからのアップロードを可能にする場合は次も追加

  # ====================================
  # Paper Clip upload from url
  # ====================================
  attr_accessor :image_url    
  before_validation :download_remote_image, :if => :image_url_provided?
  validates_presence_of :image_remote_url, :if => :image_url_provided?, :message => 'is invalid or inaccessible'

  private
 
  def image_url_provided?
    !self.image_url.blank?
  end
 
  def download_remote_image
    self.image = do_download_remote_image
    self.image_remote_url = image_url
  end
 
  def do_download_remote_image
    io = open(URI.parse(image_url))
    def io.original_filename; base_uri.path.split('/').last; end
    io.original_filename.blank? ? nil : io
    rescue # catch url errors with validations instead of exceptions (Errno::ENOENT, OpenURI::HTTPError, etc...)
  end
フォーム
<% form_for ([:admin, @page]), :html => {:multipart => true } do |f| %>
  <%= f.file_field(:image) %>
  ...or provide a URL: <%= f.text_field :image_url %>
<% end %>
表示
<%= image_tag @foobar.image.url(:m) if @foobar.image? %>