Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Rails Camp

Eric Wu
October 17, 2015

Rails Camp

Information Technology Association Club - ITAC

Eric Wu

October 17, 2015
Tweet

Other Decks in Education

Transcript

  1. Eric (@eric0324) • C++ • Ruby • Ruby on Rails

    • Android • PHP • Python • JavaScript • One Piece • FB:fb.me/eric0324
  2. 認識⼯工具 • 編輯器 • Sublime、TextAtom、Bracketst、Vim 以及 Emacs都是你 可以⽤用來來寫程式的好⼯工具。 • 命令提⽰示字元

    • 啟動 Rails 伺服器與執⾏行行命令的好夥伴。 • 瀏覽器 • 火狐、Safari、Chrome可以⽤用來來預覽你的網站。
  3. 設計 打開 app/views/layouts/application.html.erb,找到這⾏行行 <%= stylesheet_link_tag "application", media: "all", "data-turbolinks-track" =>

    true %> 在這⾏行行上⾯面加入 <link rel="stylesheet" href=“//netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap.min.css"> <link rel="stylesheet" href="//netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap-theme.min.css">
  4. 設計 讓我們也加入選單與 footer 到⾴頁⾯面吧。 在同⼀一個檔案裡,<body> 標籤下⾯面,加入 <nav class="navbar navbar-default" role="navigation">

    <div class="container-fluid"> <div class="navbar-header"> <button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1"> <span class="sr-only">Toggle navigation</span> <span class="icon-bar"></span> <span class="icon-bar"></span> <span class="icon-bar"></span> </button> <a class="navbar-brand" href="/">The Idea app</a> </div> <div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1"> <ul class="nav navbar-nav"> <li class="active"><a href="#">Link</a></li> </ul> </div> </div> </nav>
  5. 設計 在 </body> 之前,加入 <footer> <div class="container"> ITAC Rails 2015

    </div> </footer> <script src="//netdna.bootstrapcdn.com/bootstrap/3.1.1/js/ bootstrap.min.js"></script>
  6. 設計 footer { margin-top: 100px; } td { text-align: center;

    padding-left: 10px; } th { border-bottom: 1px solid #DDD; padding- left: 20px; } 改變⼀一下 ideas 表格的風格。 打開 app/assets/stylesheets/application.css 到最下⾯面加入
  7. 建立 Comment Scaffold 有留留⾔言者的姓名、留留⾔言內容以及給哪個 idea 留留⾔言 rails g scaffold comment

    user_name:string body:text idea_id:integer 在資料庫建立 comment rake db:migrate
  8. 建立 Model 的 Relation 要確定 Rails 知道 ideas 與 comments

    之間的關係 打開 app/models/idea.rb,在這⾏行行的後⾯面: class Idea < ActiveRecord::Base 加入 has_many :comments
  9. 加入留留⾔言表單 打開 app/views/ideas/show.html,並在 image_tag 之後 <%= image_tag(@idea.picture_url, :width => 600)

    if @idea.picture.present? %> 加入 <h3>Comments</h3> <% @comments.each do |comment| %> <div> <strong><%= comment.user_name %></strong> <br /> <p><%= comment.body %></p> </div> <% end %> <%= render 'comments/form' %>
  10. 設計 idea 清單 打開 app/views/ideas/index.html.erb 把整個檔案換成下⾯面這段程式碼: <h1>Listing ideas</h1> <% @ideas.in_groups_of(3)

    do |group| %> <div class="row"> <% group.compact.each do |idea| %> <div class="col-md-4"> <%= image_tag idea.picture_url, width: '100%' if idea.picture.present?%> <h4><%= link_to idea.name, idea %></h4> <%= idea.description %> </div> <% end %> </div> <% end %>
  11. 設計 idea 細節⾴頁⾯面 打開 app/views/ideas/show.html.erb 把整個檔案的內容換成下⾯面的程式碼: <p id="notice"><%= notice %></p>

    <div class="row"> <div class="col-md-9"> <%= image_tag(@idea.picture_url, width: "100%") if @idea.picture.present? %> </div> <div class="col-md-3"> <p><b>Name: </b><%= @idea.name %></p> <p><b>Description: </b><%= @idea.description %></p> <p> <%= link_to 'Edit', edit_idea_path(@idea) %> | <%= link_to 'Destroy', @idea, data: { confirm: 'Are you sure?' }, method: :delete %> | <%= link_to 'Back', ideas_path %> </p> </div> </div>
  12. 設定 Devise 打開 app/views/layouts/application.html.erb 找到 <%= yield %>,並在上⾯面加入這段程式碼: <% if

    notice %> <p class="alert alert-success"><%= notice %></p> <% end %> <% if alert %> <p class="alert alert-danger"><%= alert %></p> <% end %>
  13. 加入註冊與登入連結 編輯 app/views/layouts/application.html.erb,在這段的後⾯面 <ul class="nav"> <li class="active"><a href="/ideas">Ideas</a></li> </ul> 加入

    <p class="navbar-text pull-right"> <% if user_signed_in? %> Logged in as <strong><%= current_user.email %></strong>. <%= link_to 'Edit profile', edit_user_registration_path, :class => 'navbar-link' %> | <%= link_to "Logout", destroy_user_session_path, method: :delete, :class => 'navbar-link' %> <% else %> <%= link_to "Sign up", new_user_registration_path, :class => 'navbar-link' %> | <%= link_to "Login", new_user_session_path, :class => 'navbar-link' %> <% end %> </p>