1 分鐘閱讀

1. check the version

ruby -v
rails -v

2. create a project

gem install rails -v 4.2.0 --no-ri  # "--no-ri" means do not install documentations
rails _4.2.0_ new rails101 -T  # "-T" means do not install test units

3. initialize the git

cd rails101
git init
git add .
git commit -m "initial Commit"

4. Build a group

  1. Create controller: groups
rails g controller groups
  1. Create model: group
rails g model group title:string description:text
rake db:migrate
  1. Add a route into route.rb
# [path] config/routes.rb 
root 'groups#index'
resources :groups
  1. deal with the actions and views

start the server rails s and goto http://localhost:3000/

Unknown action “index”

# [path] app/controllers/groups_controller.rb
def index
end

Missing template groups/index

<!-- [path]  app/views/groups/index.html.erb -->
<h1>Hello World</h1>
  1. dress up the view by bootstrap
# [path] gemfile
gem 'bootstrap-sass'
bundle install
mv app/assets/stylesheets/application.css app/assets/stylesheets/application.css.scss
/* [path] app/assets/stylesheets/application.css.scss */
@import "bootstrap-sprockets";
@import "bootstrap";
  1. Add a Navbar and a Footer
<!-- [path] app/views/common/_navbar.html.erb -->
<nav class="navbar navbar-default" role="navigation">
  <div class="container-fluid">
    <!-- Brand and toggle get grouped for better mobile display -->
    <div class="navbar-header">
      <a class="navbar-brand" href="/">Rails 101</a>
    </div>

    <!-- Collect the nav links, forms, and other content for toggling -->
    <div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
      <ul class="nav navbar-nav navbar-right">
        <li> <%= link_to("登入", '#') %> </li>
      </ul>
    </div><!-- /.navbar-collapse -->
  </div><!-- /.container-fluid -->
</nav>
<!-- [path] app/views/common/_footer.html.erb -->
<footer class="container" style="margin-top: 100px;">
  <p class="text-center">Copyright ©2016 Rails101
    <br>Design by <a href="courses.growthschool.com/courses/rails-101/" target=_new>xdite</a>
  </p>
</footer>
  1. modify application.html.erb
<!-- [path] app/views/layouts/application.html.erb  -->
<body>
  <div class="container-fluid">
    <%= render "common/navbar" %>
    <%= yield %>
  </div>
  <%= render "common/footer" %>
</body>

留言