over 2 years ago

備註1:上傳Heroku的時候會遇到一些warning,作者建議看 Section 7.5
重點2:Box 3.1. Undoing things:不小心做錯了怎麼辦?
有兩個方法:generate vs destroydb:migrate vs db:rollback,還有db:migrate VERSION=0
$ rails generate controller StaticPages home help
$ rails destroy controller StaticPages home help
$ rails generate model User name:string email:string
$ rails destroy model User
$ bundle exec rake db:migrate
$ bundle exec rake db:rollback
$ bundle exec rake db:migrate VERSION=0
重點3: 教簡單的test方法:改變title,還有簡單的refactor,refactor之後,要記得再測試一次。
備註4: Test-driven development uses a “Red, Green, Refactor” cycle.
備註5: embedded Ruby, also called ERb.
重點6:如何使用<% provide(:title, "Home")%>
重點7:講到application.html.erb

  1. update the gems
    $ bundle update

  2. flags for “all changes” (-a) and a message (-m).
    Git also lets us roll the two flags into one using git commit -am "Message".
    $ git commit -am "Improve the README"
    Section 3.1都是準備工作:

  3. 建一個新的repo,然後上傳。
    create a new repository at Bitbucket
    $ git remote add origin git@bitbucket.org:<username>/sample_app.git
    $ git push -u origin --all # pushes up the repo and its refs for the first time

  4. commit之後,再新增一個Heroku,然後再上傳
    $ git commit -am "Add hello"
    $ heroku create
    $ git push heroku master

  5. If you run into problems at Heroku, make sure to take a look at the production logs to try to diagnose the problem:
    $ heroku logs
    Heroku的網站上面也可以看到

  6. 可能會做大幅度的修改時,記得先弄一個新的branch出來。
    Recall from Section 1.4.4 that, when using Git, it’s a good practice to do our work on a separate topic branch rather than the master branch. If you’re using Git for version control, you should run the following command to checkout a topic branch for static pages:
    $ git checkout master
    $ git checkout -b static-pages
    (The first line here is just to make sure that you start on the master branch, so that the static-pages topic branch is based on master. You can skip that command if you’re already on the master branch.)
    checkout -b可以同時新增跟轉換到新的branch

  7. Controller的名字要用駱駝:StaticPages。後面是action,可以直接用小寫。
    $ rails generate controller StaticPages home help
    附註:這樣會同時產生一些相關連的檔案,不過controller裡面只會有home跟help這兩個actions,其他像是standard REST actions中的index、show、new、edit、create、update、destroy都不會有。
    而且還會自動去修改routes.rb
    routes.rb會多出來兩個,defines the correspondence between URLs and web pages
    如果後面沒有接action,可能就變成resource::staticpages

    Rails.application.routes.draw do
    get 'static_pages/home'
    get 'static_pages/help'
    end
    
  8. commit 新的 branch
    $ git push -u origin static-pages
    之後可以用git push

  9. StaticPages 跟 static_pages ,其實產生的controller都會是static_pages_controller.rb

  10. Box 3.1. Undoing things:不小心做錯了怎麼辦?
    $ rails generate controller StaticPages home help
    $ rails destroy controller StaticPages home help
    $ rails generate model User name:string email:string
    $ rails destroy model User
    $ bundle exec rake db:migrate
    $ bundle exec rake db:rollback
    $ bundle exec rake db:migrate VERSION=0
    從0開始,where the version numbers come from listing the migrations sequentially.

  11. POST is the request sent by your browser when you submit a form.
    POST requests are typically used for creating things

  12. There is an especially lively debate over the use of test-driven development (TDD),6 a testing technique in which the programmer writes failing tests first, and then writes the application code to get the tests to pass.
    TDD:先做錯的,然後再把它改正。

  13. rails generate controller會自動幫我們產生一個測試檔 (a test file):
    assert_response

    $ ls test/controllers/
    static_pages_controller_test.rb
    
    test/controllers/static_pages_controller_test.rb
    require 'test_helper'
    
    class StaticPagesControllerTest < ActionController::TestCase
    
      test "should get home" do
    get :home
    assert_response :success
    end
    
      test "should get help" do
    get :help
    assert_response :success
    end
    end
    
  14. rake test來執行test

    $ bundle exec rake test
    2 tests, 2 assertions, 0 failures, 0 errors, 0 skips
    
  15. rake test來執行test,如果有錯誤,就會出現錯誤訊息:
    這邊的例子是先出現routing錯誤,又出現action錯誤,然後才是view錯誤

    $ bundle exec rake test
    ActionController::UrlGenerationError:
    No route matches {:action=>"about", :controller=>"static_pages"}
    
    $ bundle exec rake test
    AbstractController::ActionNotFound:
    The action 'about' could not be found for StaticPagesController
    
    $ bundle exec rake test
    ActionView::MissingTemplate: Missing template static_pages/about
    
  16. 測試title:assert_select

    test/controllers/static_pages_controller_test.rb
     require 'test_helper'
    
    class StaticPagesControllerTest < ActionController::TestCase
    
    test "should get home" do
    get :home
    assert_response :success
    assert_select "title", "Home | Ruby on Rails Tutorial Sample App"
    end
    
    test "should get help" do
    get :help
    assert_response :success
    assert_select "title", "Help | Ruby on Rails Tutorial Sample App"
    end
    
    test "should get about" do
    get :about
    assert_response :success
    assert_select "title", "About | Ruby on Rails Tutorial Sample App"
    end
    
  17. 測試結果會是錯誤

    $ bundle exec rake test
    3 tests, 6 assertions, 3 failures, 0 errors, 0 skips
    

    <title>Home | Ruby on Rails Tutorial Sample App</title>一個一個加到HTML之後,就會正確了。

  18. <% provide(:title, "Home") %><title><%= yield(:title) %> | Ruby on Rails Tutorial Sample App</title>
    這個時候,$ bundle exec rake test還是會是正確的。

    app/views/static_pages/home.html.erb
     <% provide(:title, "Home") %>
    <!DOCTYPE html>
    <html>
    <head>
    <title><%= yield(:title) %> | Ruby on Rails Tutorial Sample App</title>
    </head>
    <body>
    <h1>Sample App</h1>
    <p>
      This is the home page for the
      <a href="http://www.railstutorial.org/">Ruby on Rails Tutorial</a>
      sample application.
    </p>
    </body>
    </html>
    
  19. app/views/layouts/application.html.erb

    <title><%= yield(:title) %> | Ruby on Rails Tutorial Sample App</title>
    
  20. .erb的內容會轉換成HTML,然後放到<%= yield %>裡面
    visiting the page /static_pages/home converts the contents of home.html.erb to HTML and then inserts it in place of <%= yield %>.

  21. <%= stylesheet_link_tag ... %>
    <%= javascript_include_tag "application", ... %>
    <%= csrf_meta_tags %>
    

    This code arranges to include the application stylesheet and JavaScript, which are part of the asset pipeline (Section 5.2.1), together with the Rails method csrf_meta_tags, which prevents cross-site request forgery (CSRF), a type of malicious web attack.

  22. 要記得把<!DOCTYPE html>,,

    ,這些HTML的結構清掉,才能放到.erb裡面。
    Of course, the views in Listing 3.28, Listing 3.30, and Listing 3.31 are still filled with all the HTML structure included in the layout, so we have to remove it, leaving only the interior contents.
  23. 改成root 'static_pages#home',會讓原本的static_pages/home失效

← Git - Bitbucket - Heroku 步驟 ROR TUTORIAL (3RD ED.) Ch4 Rails-flavored Ruby →