Subscribed unsubscribe Subscribe Subscribe

Padrinoのインストールと公式サイトのsample_blogアプリの作成

rubyでPadrinoをインストールしてみる

padrinoのインストール

$ sudo gem install padrino

Projectの作成

$ padrino g project sample_blog -t shoulda -e haml -c sass -s jquery -d activerecord -b

今回はあくまで公式サイト通りに作成します。
個人的にはテンプレートエンジンはhamlではなく、erbを推奨します。

先ほど作成されたsample_blogディレクトリに移動する

$ cd sample_blog

Admin Dashboard のセットアップ

$ padrino g admin

DBのインストールと作成

$ padrino rake ar:create ar:migrate seed

padrino起動

$ padrino start

ブラウザでアクセス

http://localhost:3000/admin/

f:id:keiwt:20140914222342p:plain

modelを作成して、入稿できるようにする

$ padrino-gen model post title:string body:string

これでpost.rbモデルとpostsテーブルのマイグレーションファイルが作成されます。

DB更新(postsテーブルの作成)

$ padrino rake ar:migrate

postsコントローラーの作成

$ padrino-gen controller posts get:index get:show

indexアクションに対応するviewの作成

# app/views/posts/index.haml
- @title = "Welcome"

#posts= partial 'posts/post', :collection => @posts

includeファイルの作成

# app/views/posts/_post.haml
.post
  .title= link_to post.title, url_for(:posts, :show, :id => post)
  .date= time_ago_in_words(post.created_at || Time.now) + ' ago'
  .body= simple_format(post.body)

showアクションに対応するviewの作成

# app/views/posts/show.haml
- @title = @post.title
#show
  .post
    .title= @post.title
    .date= time_ago_in_words(@post.created_at || Time.now) + ' ago'
    .body= simple_format(@post.body)    
%p= link_to 'View all posts', url_for(:posts, :index)

postページの作成

$ padrino-gen admin_page post

起動

padrino start

入稿してみる

http://localhost:3000/admin/posts/new

f:id:keiwt:20140914223914p:plain

http://localhost:3000/admin/

f:id:keiwt:20140914223446p:plain

投稿にアカウントを紐付ける

$ padrino-gen migration AddAccountToPost account_id:integer

validationの追加

# models/post.rb
class Post < ActiveRecord::Base
  belongs_to :account
  validates_presence_of :title
  validates_presence_of :body
end

DBマイグレーション

$ padrino rake ar:migrate

admin/controllers/posts.rbの編集

SampleBlog::Admin.controllers :posts do
  get :index do
    @title = "Posts"
    @posts = Post.all
    render 'posts/index'
  end

  get :new do
    @title = pat(:new_title, :model => 'post')
    @post = Post.new
    render 'posts/new'
  end

  post :create do
    @post = Post.new(params[:post])
#  @post.account = current.account # こいつを追記するがエラーになるので、追記しない
    if @post.save
      @title = pat(:create_title, :model => "post #{@post.id}")
      flash[:success] = pat(:create_success, :model => 'Post')
      params[:save_and_continue] ? redirect(url(:posts, :index)) : redirect(url(:posts, :edit, :id => @post.id))
    else
      @title = pat(:create_title, :model => 'post')
      flash.now[:error] = pat(:create_error, :model => 'post')
      render 'posts/new'
    end
  end

  get :edit, :with => :id do
    @title = pat(:edit_title, :model => "post #{params[:id]}")
    @post = Post.find(params[:id])
    if @post
      render 'posts/edit'
    else
      flash[:warning] = pat(:create_error, :model => 'post', :id => "#{params[:id]}")
      halt 404
    end
  end

  put :update, :with => :id do
    @title = pat(:update_title, :model => "post #{params[:id]}")
    @post = Post.find(params[:id])
    if @post
      if @post.update_attributes(params[:post])
        flash[:success] = pat(:update_success, :model => 'Post', :id =>  "#{params[:id]}")
        params[:save_and_continue] ?
          redirect(url(:posts, :index)) :
          redirect(url(:posts, :edit, :id => @post.id))
      else
        flash.now[:error] = pat(:update_error, :model => 'post')
        render 'posts/edit'
      end
    else
      flash[:warning] = pat(:update_warning, :model => 'post', :id => "#{params[:id]}")
      halt 404
    end
  end

  delete :destroy, :with => :id do
    @title = "Posts"
    post = Post.find(params[:id])
    if post
      if post.destroy
        flash[:success] = pat(:delete_success, :model => 'Post', :id => "#{params[:id]}")
      else
        flash[:error] = pat(:delete_error, :model => 'post')
      end
      redirect url(:posts, :index)
    else
      flash[:warning] = pat(:delete_warning, :model => 'post', :id => "#{params[:id]}")
      halt 404
    end
  end

  delete :destroy_many do
    @title = "Posts"
    unless params[:post_ids]
      flash[:error] = pat(:destroy_many_error, :model => 'post')
      redirect(url(:posts, :index))
    end
    ids = params[:post_ids].split(',').map(&:strip)
    posts = Post.find(ids)

    if Post.destroy posts

      flash[:success] = pat(:destroy_many_success, :model => 'Posts', :ids => "#{ids.to_sentence}")
    end
    redirect url(:posts, :index)
  end
end

show.hamlの編集

# app/views/posts/show.haml
- @title = @post.title
#show
  .post
    .title= @post.title
    .date= time_ago_in_words(@post.created_at || Time.now) + ' ago'
    .body= simple_format(@post.body)
    .details # こいつを追記
      .author Posted by #{@post.account.email} # こいつを追記
%p= link_to 'View all posts', url_for(:posts, :index)

_post.hamlの編集

.post
  .title= link_to post.title, url_for(:posts, :show, :id => post)
  .date= time_ago_in_words(post.created_at || Time.now) + ' ago'
  .body= simple_format(post.body)
  .details
    .author Posted by #{@post.account.email}

padrinoの起動

padrino start

application.hamlの作成

# app/views/layouts/application.haml
!!! Strict
%html
  %head
    %title= [@title, "Padrino Sample Blog"].compact.join(" | ")
    = stylesheet_link_tag 'reset', 'application'
    = javascript_include_tag 'jquery', 'application'
    = yield_content :include
  %body
    #header
      %h1 Sample Padrino Blog
      %ul.menu
        %li= link_to 'Blog', url_for(:posts, :index)
        %li= link_to 'About', url_for(:about)
    #container
      #main= yield
      #sidebar
        - form_tag url_for(:posts, :index), :method => 'get'  do
          Search for:
          = text_field_tag 'query', :value => params[:query]
          = submit_tag 'Search'
        %p Recent Posts
        %ul.bulleted
          %li Item 1 - Lorem ipsum dolorum itsum estem
          %li Item 2 - Lorem ipsum dolorum itsum estem
          %li Item 3 - Lorem ipsum dolorum itsum estem
        %p Categories
        %ul.bulleted
          %li Item 1 - Lorem ipsum dolorum itsum estem
          %li Item 2 - Lorem ipsum dolorum itsum estem
          %li Item 3 - Lorem ipsum dolorum itsum estem
        %p Latest Comments
        %ul.bulleted
          %li Item 1 - Lorem ipsum dolorum itsum estem
          %li Item 2 - Lorem ipsum dolorum itsum estem
          %li Item 3 - Lorem ipsum dolorum itsum estem
    #footer
      Copyright (c) 2009-2010 Padrino

padrino起動

padrino start