Padrino とは、 Sinatra 上に構築された フル・スタック (full-stack) の ruby フレームワーク.必要な機能を選んで取り込めるのが便利.
この Web ページで行うこと
【関連する外部ページ】 http://www.padrinorb.com/guides
オプションで、次のように設定する
padrino generate project hoge -t shoulda -e erb -c sass -s jquery -d activerecord
末尾に「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"
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
「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
ここで生成するモデル
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 の管理ページに自由にアクセスできる設定
◆ 変更前
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
次の部分を削除
<%= link_to tag_icon(:user), url(:accounts, :edit, :id => current_account.id), :title => pat(:profile), :class => 'navbar-nav-link' %>
padrino start
http://localhost:3000/admin/hs の例
delete 操作を行なっているところ
さらにモデルを追加してみる
ここで生成するモデル
padrino generate model product id:integer name:text price:integer qty:integer
padrino rake ar:migrate
padrino generate admin_page product
padrino rake routes
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
http://localhost:3000/admin/products
パラメータを取得して、erb ファイルで表示してみる
params = <%= params %>
http://localhost:3000/admin/products?id=1-2
http://localhost:3000/admin/products?id=1-2,3,4,color=hoge