2013年4月21日日曜日

Paperclipライブラリ


railsの画像Upload用ライブラリをつかってみる。
  • paperclip インストール
gemファイルに追記

#Rails file upload library
gem "paperclip", "~> 3.0"

その後

bundle install

  • ImageMagick インストール
aptitude install imagemagick

で、imagemagickのpathをpaperclipに覚えさせる。
今は開発用環境なので、

config/development/environment.rb

で、最後に

Paperclip.options[:command_path] = "ImageMagickのインストールPath"

としてやる

  • Model修正
class Photo < ActiveRecord::Base
  attr_accessible :received, :shooting, :title
  attr_accessible :picture
  has_attached_file :picture, :styles => { :medium => "300x300>", :thumb => "100x100>"} 
end

  • マイグレーションファイル修正 (db/migrate/初回migration日時_create_photos.rb)
class Photo < ActiveRecord::Base
class CreatePhotos < ActiveRecord::Migration
  def change
    create_table :photos do |t|
      t.datetime :shooting
      t.datetime :received
      t.string :title
      t.attachment :picture   #Add

      t.timestamps
    end
  end
end

bundle exec rake db:migrate

で、あたらしく項目を追加してあげる。

また、

rails generate paperclip Model名 (ファイルattachする)フィールド名

で、1テーブルへのファイルattouch/detouchができるようにする。


  • Viewの変更
デフォルトFormに追加
  
<%= f.label :picture %>
<%= f.file_field :picture %>

エントリの方

<%= image_tag @photo.picture.url(:thumb) %>




2013年4月11日木曜日

Rails立ち上げではまったこと

Ubuntu 12.10にRuby on railsをしてインストールしようとしてはまったので、メモ。
  • ruby
以下からソース持ってきてbuild
ダウンロードフォルダに行って、
./configure
make
sudo make install

  • Rails
gem install rails

  • SQLite
以下からソース持ってきてbuild
http://www.sqlite.org/releaselog/3_7_16_1.html
ダウンロードフォルダに行って、
./configure
make
sudo make install

  • SQLite用ドライバ
gem install sqlite3


で、railsの新規アプリを作り
rails new railsapp

Webrick(軽量HTTPサーバ)を起動しようとする
rails server

と、エラー。
どうもうまくbundleされてきていないらしい。

解決法。

openssl 諸々を
sudo apt-get install xxxx
で入れる。

railsの新規アプリ内のフォルダに入って、Gemfileを編集
gem 'therubyracer'
を最後に書く

sudo aptitude install  build-essential g++
でg++をインストール。

その後
sudo bundle install

で、Webrick起動。

2011年3月27日日曜日

2011年2月28日月曜日

JSONデータへのアクセス方法

JSON形式でデータを持ってくる時の使い方:

                JSONObject jO = new JSONObject(rtn);
                JSONObject jO2 = jO.getJSONObject("response");
                JSONArray    jA = jO2.getJSONArray("groups");
                JSONObject jO3 = jA.getJSONObject(0);
                JSONArray jA2 =  jO3.getJSONArray("items");


これは、Foursquareのvenues/search APIでデータを持ってくる時の指定の仕方。
※ 階層は一個一個掘らないといけない (途中抜かしでnameを指定してもダメ)
無精をしてはいけない。

Tips: AndroidでJSONを使おう

2011年2月24日木曜日

複数packageを1apkとして提供する方法

複数packageを1apkとして提供する場合は、Manifestファイルの書き方が肝。
<manifest ....
  package = com.aaa >
  ....
   <activity android:name=".bbb.cccActivity" .... />
   <activity android:name=".ddd.eeeActivity" .... />
</manifest>

のように書いてやる。
そうするとリソースが見つからないエラーが消える。

メインパッケージ以外のクラスファイルをManifest.xmlへ記述する方法

2011年2月7日月曜日

外部Strageへの書き込み

sdcardへの書き込みではまっていたので記録を残しておく。


            FileOutputStream fileOutputStream = null;
            try {
                fileOutputStream = new FileOutputStream("/sdcard/" + dataName);
                fileOutputStream.write(data);
            } catch (Exception e) {
                cameraRelease();
            } finally {
                if (fileOutputStream != null) {
                    fileOutputStream.close();
                }
            }


で、NullPointerでおっこっていたのだが(2.1ではおけ)
2.2からは、uses-permissionに、WRITE_EXTERNAL_STRAGE を指定しなければなりません。
トホホ・・・

2011年1月14日金曜日

Animation-listの動作

androidでアニメーションさせたい場合は、res/animにxmlを作成すれば良いのだが、Tweenとanimation-listでは起動時から再生したい場合、呼び出すタイミングが違うので注意。

tweenの方は、親のActivityのOnCreateのタイミングでloadAnimationを呼んでもOK
animation-listで実行する場合は、OnCreateでstartを呼んでも実行されない。
で、どこで呼ぶかというと、ActivityのOnWindowChangedで呼ぶ。

参考リンク:以下の最後の方に記載してある。
2D Graphics