Rails中Bootstrap的安装和使用

25 篇文章 0 订阅

眼看着前端攻城师们都开始使用Bootstrap创作网页,于是也想学着在最近正在学习的Rails中使用Bootstrap。

具体安装使用过程如下:

1、创建Rails工程

rails new usedschool


2、生成脚手架

rails g scaffold Item title:string description:text --skip-stylesheets


3、应用迁移

rake db:migrate


4、修改Gemfile文件,加入要安装的bootstrap相关包



5、安装相关包

bundle install

在这里我遇到了一个Ruby版本限制的问题,将Ruby升级之后才顺利安装好了bootstrap相关包。

具体升级操作请查看这里:Ruby升级


6、运行rails g bootstrap:install生成bootstrap相关文件


到目前为止,bootstrap已经成功安装,下面来看下使用方法

7、修改application.html.erb布局文件

这一步修改模版,毕竟对bootstrap具体用法还不熟悉,所以直接拿来主义,找模版过来套用。

套用模版http://www.bootcss.com/examples/fluid.html

<!DOCTYPE html>
<html>
<head>
  <title>一起二</title>
  <%= stylesheet_link_tag    "application", :media => "all" %>
  <%= javascript_include_tag "application" %>
  <%= csrf_meta_tags %>
</head>
<body>

    <div class="navbar navbar-inverse navbar-fixed-top">
      <div class="navbar-inner">
        <div class="container-fluid">
          <button type="button" class="btn btn-navbar" data-toggle="collapse" data-target=".nav-collapse">
            <span class="icon-bar"></span>
            <span class="icon-bar"></span>
            <span class="icon-bar"></span>
          </button>
          <a class="brand" href="#">一起二</a>
          <div class="nav-collapse collapse">
            <p class="navbar-text pull-right">
              Logged in as <a href="#" class="navbar-link">Username</a>
            </p>
            <ul class="nav">
              <li class="active"><a href="#">Home</a></li>
              <li><a href="#about">About</a></li>
              <li><a href="#contact">Contact</a></li>
            </ul>
          </div><!--/.nav-collapse -->
        </div>
      </div>
    </div>

    <div class="container-fluid">
      <div class="row-fluid">
        <div class="span3">
          <div class="well sidebar-nav">
            <ul class="nav nav-list">
              <li class="nav-header">Sidebar</li>
              <li class="active"><a href="#">Link</a></li>
              <li><a href="#">Link</a></li>
              <li><a href="#">Link</a></li>
              <li><a href="#">Link</a></li>
              <li class="nav-header">Sidebar</li>
              <li><a href="#">Link</a></li>
              <li><a href="#">Link</a></li>
              <li><a href="#">Link</a></li>
              <li><a href="#">Link</a></li>
              <li><a href="#">Link</a></li>
              <li><a href="#">Link</a></li>
              <li class="nav-header">Sidebar</li>
              <li><a href="#">Link</a></li>
              <li><a href="#">Link</a></li>
              <li><a href="#">Link</a></li>
            </ul>
          </div><!--/.well -->
        </div><!--/span-->
        <div class="span9">
         	 <%= yield %>
          </div><!--/row-->
        </div><!--/span-->
      </div><!--/row-->

      <hr>

      <footer>
        <p>© By lissdy 2013</p>
      </footer>

    </div><!--/.fluid-container-->

    <!-- Le javascript
    ================================================== -->
    <!-- Placed at the end of the document so the pages load faster -->
    <script src="//cdnjs.bootcss.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
    <script src="//cdnjs.bootcss.com/ajax/libs/twitter-bootstrap/2.3.1/js/bootstrap.min.js"></script>

  </body>
</html>


8、修改Item整体页面风格

rails g bootstrap:themed items -f

运行结果:




肿么样?比Rails直接生成的好看多了吧偷笑





  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
Bootstrap 3 和 Rails 4(样例用的是Ruby 2.1.1,Rails 4.1.4) Table of Contents Preface 1 Chapter 1: Introducing Web Application Development in Rails 7 Why Bootstrap with Rails? 8 Setting up a Todo application in Rails 8 Analyzing folder structure of a Rails application 10 Creating views 12 Styling views using CSS 14 Redesigning the Todo application 16 Challenges in styling a Rails application traditionally 23 Summary 24 Chapter 2: Introducing Bootstrap 3 25 What is Bootstrap? 26 Installing Bootstrap in a Rails project 27 Placing Bootstrap files in a Rails project 28 Bootstrap – the Sass way 33 Bootstrap through CDN 35 Summary 36 Chapter 3: Powering a Rails App with Bootstrap's Grid System 37 What is Bootstrap's grid system? 38 Nesting columns 45 Implementing Bootstrap's grid system in a Rails application 46 Summary 54  Table of Contents Chapter 4: Using Bootstrap's Typography, Buttons, and Images in a Rails Application 55 Setting up 56 Styling typography 57 Aligning text 61 Text transformation 62 Blockquotes 62 Styling listing elements 64 Creating and styling buttons 67 Which elements are eligible to use Bootstrap's button classes? 70 Styling images in Bootstrap 70 Summary 72 Chapter 5: Reinventing Tables and Forms in Bootstrap 73 Creating Bootstrap tables 73 Creating Bootstrap forms 79 Validation classes in Bootstrap forms 84 Adding Bootstrap table a to our Rails application 85 Adding Bootstrap forms to our Rails application 90 Summary 94 Chapter 6: Creating Navigation Bars 95 Getting started with a navigation bar 95 Navigation bar helper classes 102 Adding a navigation bar to the Rails application 103 Summary 105 Chapter 7: Various Other Bootstrap Components 107 Bootstrap Breadcrumbs 107 The pagination component 108 Bootstrap labels and badges 109 Bootstrap jumbotrons 110 Alerts 111 Creating a progress bar 112 Panels 114 Summary 116 Chapter 8: Working with Bootstrap Modals 117 Getting started with modals 117 Changing Bootstrap's modal size 121 Additional Bootstrap modal features 122 [ ii ]  Table of Contents Fetching remote content from a Bootstrap modal 123 Using Bootstrap's modal in a Rails application 125 Summary 128 Chapter 9: Creating Image Slideshows with Bootstrap Carousel 129 Getting started with Bootstrap Carousel 130 Adding captions to the slides 138 Customizing Carousel 138 Summary 138 Chapter 10: Creating a Shopping Cart Using Bootstrap Modals 139 Adding a shopping cart symbol 139 Creating a shopping cart using modals 141 Summary 146 Appendix: Adding Custom Styles to a Rails Application 147 Adding Bootstrap-sass to a Rails application 147 Customizing Bootstrap through variables 149 Summary Index 150 151

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值