【ハウツー】
アプリケーションを無事に起動することが確認できた。それでは続けて、モデルとコントローラを作成してみよう。ここでは「product」モデルを作成する。モデル作成時に「brand」「name」「price」「quantity」「sku」の5カラムを指定した。
% rhogen model product brand,name,price,quantity,sku
Generating with model generator:
[ADDED] app/Product/index.erb
[ADDED] app/Product/edit.erb
[ADDED] app/Product/new.erb
[ADDED] app/Product/show.erb
[ADDED] app/Product/index.bb.erb
[ADDED] app/Product/edit.bb.erb
[ADDED] app/Product/new.bb.erb
[ADDED] app/Product/show.bb.erb
[ADDED] app/Product/product_controller.rb
[ADDED] app/Product/product.rb
[ADDED] app/test/product_spec.rb
app/Productディレクトリ以下にモデルファイル(product.rb)とコントローラファイル(product_controller.rb)、ビューファイル(*.erb)が作成された。
# The model has already been created by the framework, and extends Rhom::RhomObject
# You can add more methods here
class Product
include Rhom::PropertyBag
#add model specifc code here
end
require 'rho/rhocontroller'
require 'helpers/browser_helper'
class ProductController < Rho::RhoController
include BrowserHelper
#GET /Product
def index
@products = Product.find(:all)
render
end
# GET /Product/{1}
def show
@product = Product.find(@params['id'])
if @product
render :action => :show
else
redirect :action => :index
end
end
# GET /Product/new
def new
@product = Product.new
render :action => :new
end
# GET /Product/{1}/edit
def edit
@product = Product.find(@params['id'])
if @product
render :action => :edit
else
redirect :action => :index
end
end
# POST /Product/create
def create
@product = Product.new(@params['product'])
@product.save
redirect :action => :index
end
# POST /Product/{1}/update
def update
@product = Product.find(@params['id'])
@product.update_attributes(@params['product']) if @product
redirect :action => :index
end
# POST /Product/{1}/delete
def delete
@product = Product.find(@params['id'])
@product.destroy if @product
redirect :action => :index
end
end
<div id="pageTitle">
<h1>Products</h1>
</div>
<div id="toolbar">
<div id="leftItem" class="regularButton"><%= link_to "Home", Rho::RhoConfig.start_path %></div>
<div id="rightItem" class="regularButton"><%= link_to "New", :controller => :Product, :action => :new %></div>
</div>
<div id="content">
<ul>
<% @products.each do |obj| %>
<li>
<a href="<%= url_for :action => :show, :id => obj.object %>">
<span class="title"><%= obj.brand %></span>
<span class="disclosure_indicator"></span>
</a>
</li>
<% end %>
</ul>
</div>
rhogenでモデルを作成できたら、ホーム画面からリンクを貼ってみる。app/index.erbの「Add Links Here...」を変更し、Productへのリンクを貼る。
% diff -u index.erb.orig index.erb
--- index.erb.orig 2010-07-09 17:02:17.000000000 +0900
+++ index.erb 2010-07-09 17:02:36.000000000 +0900
@@ -14,10 +14,10 @@
<div id="content">
<ul>
<li>
- <a href="#">
- <span class="title">Add Links Here...</span>
+ <a href="Product">
+ <span class="title">Products</span>
<span class="disclosure_indicator"></span>
</a>
</li>
</ul>
-</div>
\ No newline at end of file
+</div>
さきほどと同様の手順で、iPhone/Android用のアプリケーションをビルドし、各エミュレータ上で動作を確認する。
ホーム画面が表示されずにNot Foundと出たり、おかしな動作をするときはいったんrake cleanを実行してみよう。
RhodesでiPhone/Androidのネイティブアプリケーションを動作させてみるところまでを紹介した。1ソースをベースに複数スマートフォンのネイティブアプリを作れるのはなかなか快感だ。公式サイトのDocsにはチュートリアルやテクニカルFAQなどの情報も充実している。コードを見ながら、実装を追ってみるのも良し。興味がある方はユーザ登録をおこなって、リファレンスをもとに、エミュレータや実機でいろいろ試してみてはいかがだろうか。
| rhomobile - the open mobile framework |
| rhomobile - the open mobile framework |
| Tutorial - Rhomobile |
| rhomobile's rhodes at master - GitHub |
| TechnicalFAQ - Rhomobile |
| 理研、脳・脊髄形成に必要な神経板湾曲の仕組みを解明 [20:16 5/25] |
| 京大、「慢性閉塞性肺疾患」患者の労作時呼吸困難は鍼治療が有効と実証 [20:08 5/25] |
| 120Hz SHVカメラ用イメージセンサーを使った撮像装置 - SHVフルスペック化へ [18:10 5/25] |
| 京大、視覚による物体認知は前頭前野からのトップダウン信号が重要と確認 [17:45 5/25] |
| 製品数の拡大だけでなくBCPの展開なども含めた総合力で事業の強化を図るTI [17:25 5/25] |
|
[AKB48]じゃんけん2位の藤江れいな、総選挙目標は「17位」 近野莉菜は姉妹グループに対抗心 [15:38 5/27] ホビー |
|
[注目の新譜]きゃりーぱみゅぱみゅ カワイイがいっぱい! 待望の初アルバムを発表 [15:30 5/27] ホビー |
|
奥瀬サキ「火閻魔人」25年ぶり再始動、新キャラも続々 [15:17 5/27] ホビー |
|
[庵野秀明監督]「ヱヴァ」進行状況を聞かれ立腹 「日プロ大賞」授賞式 [15:01 5/27] ホビー |
|
【女性編】おなかが減ったけどおかずがない! そんなときおかずになる調味料ランキング [15:00 5/27] ライフ |
4つの診断で、自分の適性を見つめなおそう!
働くこと・挑戦し続けることへの思いを綴ったインタビュー
あなたにピッタリのアドバイスを読むことができます。
転職に必要な情報が収集できます
企業からアプローチのメッセージが届きます。