Tag: Rails/Bootstrap
gem 'bootstrap-sass', '~> 3.3.6' gem 'font-awesome-sass' gem 'jquery-rails'
bundle install --path vendor/bundle
@import "bootstrap-sprockets"; @import "bootstrap"; @import "font-awesome-sprockets"; @import "font-awesome";
//= require jquery //= require bootstrap-sprockets
bundle exec rails g scaffold book title:string author:string summary:text bundle exec rake db:migrate
<nav class="navbar navbar-default" role="navigation"> <div class="container-fluid"> <div class="navbar-header"> <a class="navbar-brand" href="/"><i class="fa fa-home"></i>Home</a> </div> <div class="navbar-collapse collapse"> <ul class="nav navbar-nav"> <li><a href="http://www.google.com/" target="_blank">Google</a></li> <li><a href="http://www.yahoo.co.jp/" target="_blank">Yahoo! Japan</a></li> </ul> <ul class="nav navbar-nav navbar-left"> <div class="form-group"> <%= form_tag('/books', method: 'get', class: 'navbar-form navbar-right') do %> <%= text_field_tag(:q, params[:q], class: 'form-control', style: 'width: 200px;', placeholder: 'Search') %> <% end %> </div> </ul> </div> </div><!-- /.container-fluid --> </nav> <div class="container"> <!-- flash messages --> <%= yield %> <hr> <footer> <p>© src256</p> </footer> </div>
gem 'bootstrap-sass', '~> 3.3.6' gem 'font-awesome-sass'
bundle install --path vendor/bundle
@import "bootstrap-sprockets"; @import "bootstrap"; @import "font-awesome-sprockets"; @import "font-awesome";
//= require bootstrap-sprockets
gem 'bootstrap-sass', '~> 3.3.6' gem 'font-awesome-sass'
bundle install --path vendor/bundle
@import "bootstrap-sprockets"; @import "bootstrap"; @import "font-awesome-sprockets"; @import "font-awesome";
//= require bootstrap-sprockets
bundle exec rails g scaffold book title:string author:string summary:text bundle exec rake db:migrate
<nav class="navbar navbar-default" role="navigation"> <div class="container-fluid"> <div class="navbar-header"> <a class="navbar-brand" href="/"><i class="fa fa-home"></i>Home</a> </div> <div class="navbar-collapse collapse"> <ul class="nav navbar-nav"> <li><a href="http://www.google.com/" target="_blank">Google</a></li> <li><a href="http://www.yahoo.co.jp/" target="_blank">Yahoo! Japan</a></li> </ul> <ul class="nav navbar-nav navbar-left"> <div class="form-group"> <%= form_tag('/books', method: 'get', class: 'navbar-form navbar-right') do %> <%= text_field_tag(:q, params[:q], class: 'form-control', style: 'width: 200px;', placeholder: 'Search') %> <% end %> </div> </ul> </div> </div><!-- /.container-fluid --> </nav> <div class="container"> <!-- flash messages --> <%= yield %> <hr> <footer> <p>© src256</p> </footer> </div>
<%= form_for(@book, html: {class: 'form-horizontal'}) do |f| %> <% if @book.errors.any? %> <div id="error_explanation"> <h2>エラーが発生しました :</h2> <ul> <% @book.errors.full_messages.each do |msg| %> <li><%= msg %></li> <% end %> </ul> </div> <% end %> <div class="form-group"> <%= f.label(:title, 'タイトル', class: 'col-xs-2 control-label') %> <div class="col-xs-10"> <%= f.text_field(:title, class: 'form-control') %> </div> </div> <div class="form-group"> <%= f.label(:author, '著者', class: 'col-xs-2 control-label') %> <div class="col-xs-10"> <%= f.text_field(:author, class: 'form-control') %> </div> </div> <div class="form-group"> <%= f.label(:summary, '概要', class: 'col-xs-2 control-label') %> <div class="col-xs-10"> <%= f.text_area(:summary, rows: 14, cols: 20, class: 'form-control') %> </div> </div> <div class="form-group"> <div class="col-xs-12"> <%= f.submit('保存', class: 'btn btn-primary') %> </div> </div> <% end %>
.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; }
<% if @item.errors.any? %> <div class="alert alert-danger" role="alert"> <button class="close" data-dismiss="alert">×</button> <h2>エラーが発生しました :</h2> <ul> <% @item.errors.full_messages.each do |msg| %> <li><%= msg %></li> <% end %> </ul> </div> <% end %>
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; }
<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>© src256</p> </footer> </div>
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, '×'.html_safe, 'aria-hidden' => true) concat content_tag(:span, 'Close', class: 'sr-only') end) concat message end) end nil end end
def index @books = Book.all flash[:notice] = 'はじめまして' end
bundle exec rails g kaminari:views bootstrap3