Padrino で新しいモデル、ビュー、コントローラーの生成
Padrino とは、 Sinatra 上に構築された フル・スタック (full-stack) の ruby フレームワーク.必要な機能を選んで取り込めるのが便利.
この Web ページで行うこと
- Padrino で新しいモデルの作成
- 新しいモデルの追加
- ビューファイルを書き換えてみる
【関連する外部ページ】 http://www.padrinorb.com/guides
前準備
Padrino で新しいモデルの作成
- Padrino プロジェクトの生成
- ひな形の生成
オプションで、次のように設定する
- プロジェクト名: hoge
- orm: activerecord
- test: shoulda
- script: jquery
- renderer: erb
- stylesheet: sass
padrino generate project hoge -t shoulda -e erb -c sass -s jquery -d activerecord
- Gemfile の調整
末尾に「gem 'activesupport'」を追加する.
gem 'activesupport'
Gemfile の設定例(書きかけ)
参考のために載せています.bundle の実行で問題が出たときに参考にしてください (bundle の実行で問題がないときは、ここの記述は気にせずに続行してください)
gem 'rake' gem 'erubis', '~> 2.7.0' gem 'activerecord', '>= 3.1', :require => 'active_record' gem 'sqlite3' gem 'activesupport' gem 'redis-objects' gem 'tilt', '1.3.7' gem 'padrino', '0.11.2' gem 'thin' gem "bcrypt-ruby", :require => "bcrypt"
- bundle の実行
cd hoge bundle
bundle の実行時に次のようなエラーメッセージが出る場合がある.
Bundler could not find compatible versions for gem "tilt": In Gemfile: padrino (= 0.11.1) ruby depends on tilt (~> 1.3.0) ruby padrino (= 0.11.1) ruby depends on tilt (1.4.1)
エラーを回避するために、hoge/Gemfile を編集し、 末尾に次を追加 する
gem 'tilt', '1.3.7'
さらに、次のコマンドを実行する
sudo gem install tilt -v 1.3.7
その後 bundle を実行する。エラーメッセージが出ていないことを確認する
bundle
- ひな形の生成
- config/database.rb の確認
- ルーティング定義の確認
「padrino rake routes」で、ルーティングを確認しておく.
ルーティングが未定義であることが確認できる
padrino rake routes
rake コマンドの説明(2013/06 時点)
rake ar:abort_if_pending_migrations # Raises an error if there are pending migrations. rake ar:auto:upgrade # Uses schema.rb to auto-upgrade. rake ar:charset # Retrieves database charset. rake ar:collation # Retrieves databsae collation. rake ar:create # Creates the database as defined in config/database.yml rake ar:create:all # Creates local databases as defined in config/database.yml rake ar:drop # Drops the database for the current Padrino.env rake ar:drop:all # Drops local databases defined in config/database.yml rake ar:forward # Pushes the schema to the next version. rake ar:migrate # Migrates the database through scripts in cc/migrate. rake ar:migrate:down # Runs the "down" for a given migration VERSION. rake ar:migrate:redo # Rollbacks current migration and migrates up to version rake ar:migrate:reset # Resets your database using your migrations. rake ar:migrate:up # Runs the "up" for a given migration VERSION NUMBER rake ar:reset # Drops and recreates the database using db/schema.rb. rake ar:rollback # Rolls back the schema to previous schema version. rake ar:schema:dump # Creates a portable db/schema.rb file. rake ar:schema:load # Loads a schema.rb file into the database. rake ar:schema:to_migration # Creates a migration from schema.rb rake ar:schema:to_migration_with_reset # Creates a migration and resets the migrations log. rake ar:setup # Creates the database, loads the schema, and seeds data. rake ar:structure:dump # Dumps the database structure to a SQL file. rake ar:version # Retrieves the current schema version number.
- ダッシュボードの生成
引き続きダッシュボードの生成を行う
padrino generate admin
- 再度、ルーティング定義の確認
padrino rake routes
- モデルの生成
ここで生成するモデル
- r(key:text, field:text, value:text)
- h(A1:text, A2:text, B1:text, B2:text)
padrino generate model r key:text field:text value:text padrino generate model h A1:text A2:text B1:text B2:text
- データベースマイグレーション等を行うために、所定のコマンドを実行
新しいモデルを定義したので、データベースマイグレーションを行う.
このときデータベース管理者のメールアドレスなどを設定.
bundle padrino rake ar:create padrino rake ar:migrate padrino rake seed
- 管理ページの生成
モデル r とモデル h の管理ページを生成する
この操作で、コントローラとビューファイルができる.
padrino generate admin_page r padrino generate admin_page h
- 再度、ルーティング定義の確認
padrino rake routes
- (オプション)
ログイン操作なしで、モデル r とモデル h の管理ページに自由にアクセスできる設定
- admin/app.rb の調整
◆ 変更前
access_control.roles_for :any do |role| role.protect '/' role.allow '/sessions' end
◆ 変更後
以下のように変更する
access_control.roles_for :any do |role| role.protect '/' role.project_module :hs, '/hs' role.project_module :rs, '/rs' role.allow '/sessions' end
Padrino のロールについては、次の Web ページに説明がある. - admin/views/layouts/application.erb の調整
次の部分を削除
<%= link_to tag_icon(:user), url(:accounts, :edit, :id => current_account.id), :title => pat(:profile), :class => 'navbar-nav-link' %>
- admin/app.rb の調整
- 「padrino start」で、サーバを起動
padrino start
- http://localhost:3000/admin/rs, http://localhost:3000/admin/hs で画面が出ることを確認
http://localhost:3000/admin/hs の例
delete 操作を行なっているところ
新しいモデルの追加
さらにモデルを追加してみる
- モデルの生成
ここで生成するモデル
- product(id:integer, name:text, price:integer, qty:integer)
padrino generate model product id:integer name:text price:integer qty:integer
- 新しいモデルを作成したので、再度、マイグレーションを実行
padrino rake ar:migrate
- product の管理画面生成
padrino generate admin_page product
- 再度、「padrino rake routes」で、ルーティングを確認する
padrino rake routes
- ログイン操作なしで、モデル r とモデル h の管理ページに自由にアクセスできる設定
admin/app.rb を次のように変更する
◆ 変更後
access_control.roles_for :any do |role| role.protect '/' role.project_module :hs, '/hs' role.project_module :rs, '/rs' role.project_module :products, '/products' role.allow '/sessions' end
- 「padrino start」で、サーバを起動
padrino start
- http://localhost:3000/admin/products で画面が出ることを確認
http://localhost:3000/admin/products
ビューファイルを書き換えてみる
パラメータを取得して、erb ファイルで表示してみる
- admin/views/products/index.erb を次のように書き換える
params = <%= params %>
- テスト実行
http://localhost:3000/admin/products?id=1-2
http://localhost:3000/admin/products?id=1-2,3,4,color=hoge