ror突进-p9

切换分支

git checkout -b ch02

建立一个新的model

rails g model group title:string description:text
rake db:migrate

构建对应的groups controller

rails g controller groups

建立view

修改 app/controllers/groups_controller.rb

class GroupsController < ApplicationController
  def index
    @groups = Group.all
  end
end

建立新的页面

<div class="col-md-12">
  <div class="group">
    <%= link_to("New group", new_group_path, class: "btn btn-primary pull-right") %>
  </div>
  <table class="table table-hover">
    <thead>
      <tr>
        <td>#</td>
        <td>Title</td>
        <td>Description</td>
      </tr>
    </thead>
    <tbody>
      <% @groups.each do |group| %>
        <tr>
          <td>#</td>
          <td><%= link_to(group.title, group_path(group)) %></td>
          <td><%= group.description %></td>
          <td>
              <%= link_to("Edit", edit_group_path(group), class: "btn btn-sm btn-default")%>
              <%= link_to("Delete", group_path(group),    class: "btn btn-sm btn-default",
                          method: :delete, data: { confirm: "Are you sure?" } )%>
          </td>
        </tr>
      <% end %>
    </tbody>
  </table>
</div>

加入路由

在 root 'welcome#index' 前加上一行 resources :groups

Rails.application.routes.draw do
   resources :groups
   root 'welcome#index'
end

插入内容

rails console
Group.create(title: "Board 1", description: "Board 1 body")
Group.create(title: "Board 2", description: "Board 2 body")

把路由改成根路由

Rails.application.routes.draw do
  resources :groups
  root 'groups#index'
end

保存

git add .
git commit -m "create groups index"