と思ったのでやってみました。
gihyo.jp:第9回 SinatraとSequel・Hamlで掲示板アプリを作る
http://gihyo.jp/dev/serial/01/ruby/0009
が自分がチュートリアル(以下、見本)としてやってみみたもので、これのmodel部分をFileMakerに
置換えました。
※FileMakerやRuby、Sinatraそのものの細かい説明は省きます。
最終的な階層は以下(見本のファイルも混在してます)
---
app/ start.rb model/ comment.rb view / layout.haml index.haml fmbbs.haml style.sass
---
FileMaker部分
・bbsテーブルの作成、カラムは見本のapp/model/comment.rb内で定義しているクラス
Commentsを真似て定義。但し、posted_dateはFileMaker側ではtimestamp型で作成時に自動で
現在のタイムスタンプが入るよう設定。
Ruby部分
・app/model/comment.rbにFileMakerとの接続クラス追加
---以下をcomment.rbに追加---
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
require 'rfm' | |
class FMServer | |
def initialize() | |
@FM_CONFIG = { | |
:host => "FileMakerServerの接続先IP", | |
:account_name => "FileMakerFileのログインアカウント", | |
:password => "FileMakerFileのログインパスワード", | |
:database => "FileMakerFileの名前", | |
:ssl => false, | |
:root_cert => false, | |
} | |
@fm = Rfm::Server.new(@FM_CONFIG) | |
end | |
def listAll(lay,sortCond) | |
return @fm[@FM_CONFIG[:database]][lay].all(sortCond) | |
end | |
def create(lay,obj) | |
@fm[@FM_CONFIG[:database]][lay].create(obj) | |
end | |
end |
・hamlを編集(追加)
---app/view/fmbbs.haml
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
%form{:method=>"POST",:action => '/fmcomment'} | |
%input{:type=>"hidden",:name=>"_method",:value=>"PUT"} | |
%table | |
%tr | |
%td 名前 | |
%td | |
%input{:type=>"text",:name=>"name"} | |
%tr | |
%td タイトル | |
%td | |
%input{:type=>"text",:name=>"title"} | |
%tr | |
%td 内容 | |
%td | |
%textarea{:name=>"message",:cols=>60,:rows=>8} | |
%tr | |
%td | |
%td | |
%input{:type=>"submit"} | |
- @comments.each do |comment| | |
.comment | |
%h2= h comment.title | |
.info | |
%span.name== by #{h comment.name} | |
%span.date== (#{timestamp_text(comment.posted_date)}) | |
.message | |
== #{formatted_text(comment.message)} |
※HamlはPythonのようにインデントが文法になってるので注意!
・app/start.rbを編集
---app/start.rbを以下のように変更---
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
#/usr/bin/ruby | |
require 'rubygems' | |
require 'sinatra' | |
require 'model/comment.rb' | |
require 'sass' | |
helpers do | |
include Rack::Utils;alias_method :h, :escape_html | |
#追加 | |
def timestamp_text(date) | |
date.strftime("%Y/%m/%d %H:%M:%S") | |
end | |
#追加 | |
def formatted_text(text) | |
Rack::Utils.escape_html(text).gsub(/\n/," | |
") | |
end | |
end | |
get '/style.css' do | |
content_type 'text/css',:charset => 'utf-8' | |
sass :style | |
end | |
get '/' do | |
@comments = Comments.order_by(:posted_date.desc) | |
haml :index | |
end | |
put '/comment' do | |
Comments.create({ | |
:name => request[:name], | |
:title => request[:title], | |
:message => request[:message], | |
:posted_date => Time.now, | |
}) | |
redirect '/' | |
end | |
#追加 | |
get '/fmbbs' do | |
fm = FMServer.new | |
@comments = fm.listAll("bbs",{:sort_field => "posted_date",:sort_order => 'descend'}) | |
haml :fmbbs | |
end | |
#追加 | |
put '/fmcomment' do | |
fm = FMServer.new | |
obj = { | |
:name => request[:name], | |
:title => request[:title], | |
:message => request[:message], | |
#posted_dateは含めない | |
} | |
fm.create("bbs",obj) | |
redirect '/fmbbs' | |
end |
・rfm(lardawge-rfm)をインストールするの忘れずに!
$ sudo gem install lardawge-rfm
GitHub:https://github.com/lardawge/rfm
これでstart.rbのある位置をカレントにして
$ ruby -rubygems start.rb
これで
http://localhost:4567/fmbbs
で試せます。簡単ですね!
今回、自分がつまずいた所は、
1,rfm経由でFileMakerにレコード作成(多分編集も同じ)でtimestamp型のカラムに値を
設定するための方法が分からなかった。※DateTime型だとエラーになる、整形してもエラー
になる。なんでや!
→読み込みは出来るので、FileMaker側で作成時自動で値設定にした。
2,投稿済みデータの降順ソートがうまくいかない!
→lardawge-rfmでは降順の指定が"desc"ではなく、"descend"だった。
Railsでやるほどじゃないけどちょっと簡単にFileMakerとWebアプリつなぎたいとか、これで
どうでしょうか? 2012/10/24 編集:コード断片をGithubのGistからの読み込みに変えました。