&tag(Rails/Bootstrap);

目次†[edit]

関連ページ†[edit]

参考情報†[edit]

bootstrap-sassを使って導入†[edit]

Gemfileの編集†[edit]

bundle install実行†[edit]

application.css.scss作成など†[edit]

動作確認†[edit]

scaffold実行†[edit]

Bootstrap3対応フォーム†[edit]

フォームの生成†[edit]

エラーメッセージの見せ方†[edit]

error.png

メッセージの表示†[edit]

application.css.scssに追加†[edit]

field_with_errors {
  @extend .form-group;
  @extend .has-error;
}

.form-horizontal .field_with_errors {
  margin: 0;
}

.form-horizontal .field_with_errors:before, .form-horizontal .field_with_errors::after {
  display: block;
  clear: none;
}

application.html.erbの編集†[edit]

<div class="container">
  <!-- flash messages -->
  <div class="row">
    <div class="col-xs-12">
      <%= flash_messages %>
    </div>
  </div>
  <div class="row">
    <%= yield %>
  </div>
  <hr>
  <footer>
    <p>&#169; src256</p>
  </footer>
</div>

application_helper.rbに追加†[edit]

module ApplicationHelper
  def bootstrap_class_for flash_type
    { success: "alert-success", error: "alert-danger", alert: "alert-warning", notice: "alert-info"\
 }[flash_type.to_sym] || flash_type.to_s
  end

  def flash_messages(opts = {})
    flash.each do |msg_type, message|
      concat(content_tag(:div, message, class: "alert #{bootstrap_class_for(msg_type)} alert-dismis\
sible", role: 'alert') do
        concat(content_tag(:button, class: 'close', data: { dismiss: 'alert' }) do
          concat content_tag(:span, '&times;'.html_safe, 'aria-hidden' => true)
          concat content_tag(:span, 'Close', class: 'sr-only')
        end)
        concat message
      end)
    end
    nil
  end
end

呼び出し例†[edit]


トップ   新規 一覧 検索 最終更新   ヘルプ   最終更新のRSS