Tag: Rails/Bootstrap/古い情報

目次

関連ページ

参考情報

bootstrap-sassを使って導入(Rails 5編)

Gemfileの編集

bundle installを実行

application.css.scss作成など

bootstrap-sassを使って導入(Rails 4編)

Gemfileの編集

bundle install実行

application.css.scss作成など

動作確認

scaffold実行

Bootstrap3対応フォーム

フォームの生成

エラーメッセージの見せ方

#ref(): File not found: "error.png" at page "Rails/Bootstrap/古い情報"

メッセージの表示

application.css.scssに追加

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の編集

<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に追加

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

呼び出し例


トップ   編集 凍結 差分 履歴 添付 複製 名前変更 リロード   新規 一覧 検索 最終更新   ヘルプ   最終更新のRSS
Last-modified: 2022-04-11 (月) 15:28:16