金子邦彦研究室プログラミングRuby による Web/データベース・プログラミングPadrino で新しいモデル、ビュー、コントローラーの生成

Padrino で新しいモデル、ビュー、コントローラーの生成

Padrino とは、 Sinatra 上に構築された フル・スタック (full-stack) の ruby フレームワーク.必要な機能を選んで取り込めるのが便利.

この Web ページで行うこと

関連する外部ページhttp://www.padrinorb.com/guides

前準備

Padrino で新しいモデルの作成

  1. Padrino プロジェクトの生成
    1. ひな形の生成

      オプションで、次のように設定する

      • プロジェクト名: 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 
      

      [image]
    2. Gemfile の調整

      末尾に「gem 'activesupport'」を追加する.

      gem 'activesupport'
      

      [image]

      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"
      
    3. bundle の実行
      cd hoge
      bundle
      

      [image]

      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)
      

      [image]

      エラーを回避するために、hoge/Gemfile を編集し、 末尾に次を追加 する

      gem 'tilt', '1.3.7'
      

      さらに、次のコマンドを実行する

      sudo gem install tilt -v 1.3.7
      

      [image]

      その後 bundle を実行する。エラーメッセージが出ていないことを確認する

      bundle
      
    4. config/database.rb の確認

      [image]
  2. ルーティング定義の確認

    「padrino rake routes」で、ルーティングを確認しておく.

    ルーティングが未定義であることが確認できる

    padrino rake routes
    

    [image]

    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.
    
  3. ダッシュボードの生成

    引き続きダッシュボードの生成を行う

    padrino generate admin 
    

    [image]
    [image]
  4. 再度、ルーティング定義の確認
    padrino rake routes 
    

    [image]
  5. モデルの生成

    ここで生成するモデル

    padrino generate model r key:text field:text value:text
    padrino generate model h A1:text A2:text B1:text B2:text
    

    [image]
  6. データベースマイグレーション等を行うために、所定のコマンドを実行

    新しいモデルを定義したので、データベースマイグレーションを行う.

    このときデータベース管理者のメールアドレスなどを設定.

    bundle
    padrino rake ar:create
    padrino rake ar:migrate
    padrino rake seed 
    

    [image]
    [image]
  7. 管理ページの生成

    モデル r とモデル h の管理ページを生成する

    この操作で、コントローラとビューファイルができる.

    padrino generate admin_page r
    padrino generate admin_page h
    

    [image]
  8. 再度、ルーティング定義の確認
    padrino rake routes 
    

    [image]
  9. (オプション)

    ログイン操作なしで、モデル r とモデル h の管理ページに自由にアクセスできる設定

    1. admin/app.rb の調整

      ◆ 変更前

          access_control.roles_for :any do |role|
            role.protect '/'
            role.allow   '/sessions'
          end
      

      [image]

      ◆ 変更後

      以下のように変更する

          access_control.roles_for :any do |role|
            role.protect '/' 
            role.project_module :hs, '/hs'
            role.project_module :rs, '/rs'
            role.allow   '/sessions'
          end    
      

      [image]
      Padrino のロールについては、次の Web ページに説明がある.

      http://padrinorb.com/guides/features/padrino-admin/

    2. admin/views/layouts/application.erb の調整

      次の部分を削除

      <%= link_to tag_icon(:user), url(:accounts, :edit, :id => current_account.id), :title => pat(:profile), :class => 'navbar-nav-link' %>
      

      [image]
  10. padrino start」で、サーバを起動
    padrino start
    
  11. http://localhost:3000/admin/rs, http://localhost:3000/admin/hs で画面が出ることを確認

    http://localhost:3000/admin/hs の例

    [image]

    [image]

    delete 操作を行なっているところ

    [image]

    [image]

新しいモデルの追加

さらにモデルを追加してみる

  1. モデルの生成

    ここで生成するモデル

    padrino generate model product id:integer name:text price:integer qty:integer
    

    [image]
  2. 新しいモデルを作成したので、再度、マイグレーションを実行
    padrino rake ar:migrate
    

    [image]
  3. product の管理画面生成
    padrino generate admin_page product
    

    [image]
  4. 再度、「padrino rake routes」で、ルーティングを確認する
    padrino rake routes
    

    [image]
  5. ログイン操作なしで、モデル 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    
    

    [image]
  6. padrino start」で、サーバを起動
    padrino start
    

    [image]
  7. http://localhost:3000/admin/products で画面が出ることを確認

    http://localhost:3000/admin/products

    [image]

ビューファイルを書き換えてみる

パラメータを取得して、erb ファイルで表示してみる