over 2 years ago

RailsFun.tw 新手教學 day3 HD

00:01:48

session的生存範圍
route > controller + model > view + helper
網址 -> 先去public找檔案 -> route ->丟404
-> controller / action -> send fild
-> View

00:04:00

model and relation
一個資料如何找到其他額外資料的方式

00:05:00

path的使用

00:07:35

只能用GET(Verb)來到這個網址/hello(.:format)(URI Pattern)
打這個網址/hello會到這個地方去hihi#tester(Controller#Action)
hello(Prefix)是縮寫,在rails裡面可以用,會變成hello_path

00:09:00

as的用法

get "hello", :to => 'hihi#tester' , :as => 'hihihi'

Prefix會從hello變成hihihi
一個rename的動作,跟對外一點關係都沒用,只有內部會變

00:13:00

html.erb裡面打

<%= root_path %> 會變成/
<%= hihihi_path %> 會變成 /hello

00:15:00

yoo GET /yoo/:id(:format) yoo#show
需要id,如果只打<%= yoo_path %>
會出現 missing require keys: [:id]
需要提供id,id 其實就是一個數字

00:16:00

link_toyoo_path其實都是method,也是helper,所以後面都可以加括號

link_to() 
yoo_path()
<%= link_to('go to yoo', yoo_path(1)) %>

1可以是999,也可以是item.first

00:21:00

講collection跟member
collection => 集合體,沒有特定對象,如index
member => 指定特定對象,要有id

00:25:00

match,可以接多個Verb,
match "dog/:id/:type/:meows/:wow", :to => 'nono#meow', via => [:get, :patch, :delete]

00:25:00

W3C規定,a tag裡面只可以有GET,不能有POST跟DELETE
所以rails的做法是寫到method裡面

:method => 'post'

可以送出選單之後,真正發生的情況要看Network。
會看到General,Request跟Responds ,Form DATA

data-method='post'是來自於
application.html.erb
<%= javascript_include_tag 'application'%>
app/assets/javascripts/application.js
要有require jquery_ujs才可以動

00:37:00

FORM HELPER的陷阱
Module ActionView::Helpers::FormHelper
建議用:url

<%= form_for 接model_name :url => kitty2_path, :method => 'delete' do |f|%>
<% end %>

00:44:00

<%= form_for Item.new, :url => Kitty2_path, :method => 'delete' do |f| %>

    <%= f.text_field :name %> 
  => <input id='item_name' name = 'item[name]' type='text' />
  item[name]hash的規則
    
  <input name="single" value="1" />
    <input name="im_array[]" value="2" /> #array是一個一個塞,所以[]裡面不用給他東西

    <input name="im_array[]" value="3" />
    <input name="im_hash[im_key1]" value="?" /> #hash是key by value

    <input name="im_hash[im_key2]" value="!" />

<% end %>
Parameters: {'item'=>{'name' => 'xxxx'}, 'single' => '1', 'im_array' => [2,3], 'im_hash' => {'im_key1' => '?', 'im_key2' => '!'} }

text所有的form helper都在幫忙弄html。
可以自己寫成:

<input id="item_name" name="item[name]" type="radio"/>

不要有f.,就可以自己寫html

<input name = "wtf" />

送出後:
Parameters: {'item'=>{'name' => 'xxxx'}, 'wtf' => 'xxx'}
打params[:item][:name]
params[:wtf]
params[:single]
params[:im_array][1]
params[:im_hash][:key1]

00:52:00

讓傳統的http支援RESTful

jquery_ujs 

00:53:00 檔案做在lib,要到config/application.rb去做require

class hellokitty
end

00:58:00

hello kitty 寫好之後要做require的動作
cd/projectname/config/application.rb
做require

require 'hello_kitty'

(不要有.rb)

剛做好的時候,可以用ruby hello_kitty.rb
require之後,可以用rails c試看看HelloKitty.say

00:59:00用到view裡面去

<%= HelloKitty.say %>
server記得要重開

1:00:00

多重controller
public:外部使用者看得到
dasgboard:用戶看得到
admin:管理員看得到

1:03:00

namespace的定義是

1:06:00

不讓機器人進去
public/robots.txt
User-agent:*
Disallow: /dashboard/
使用者後台不能對外

1:07:00

開始講controller的繼承

class Dashboard::DashboardController < ApplicationController
    before_action :authenticate_user!
end

繼承Dashboard::DashboardController,就不用再before_action :authenticate_user!

Admin重新繼承ApplicationController

class Dashboard::Admin::AdminController < ApplicationController
    before_action :authenticate_manager!
end

要驗證manager才能使用功能

1:12:00

講devise
config/initializers/devise.rb
一定要在,不然不能動

1:17:00

simple captcha
不知道為什麼就噴了,1:22:00到github直接copy code
simple-captcha/lib/generators/templates/migration.rb

class CreateSimpleCaptchaData < ActiveRecord::Migration
  def self.up
    create_table :simple_captcha_data do |t|
      t.string :key, :limit => 40
      t.string :value, :limit => 6
      t.timestamps
    end
    
    add_index :simple_captcha_data, :key, :name => "idx_key"
  end

  def self.down
    drop_table :simple_captcha_data
  end
end

1:25:00

view/simple_captcha/simple_captcha.erb

1:31:00

form_tag <form></form>

專門為@item做的表單:

form_for @item do |f| end

2 key words
搜尋:rails form helper
helpers FormHelper : for @object
helpers FormTagHelper

1:34:00

if simple_captcha_valid?
    @ans = true
else 
    @ans = false
    redirect_to :action => ;index

1:37:00

乾淨的layout
app/view/layouts/application.html.erb

<%= yield %> 之後的網頁都會出現在這個地方
在這頁(application.html.erb)可以加<%= yield :header %>
然後到其它頁面寫:

<% content_for :header do %>
  這邊寫的東西就會出現在剛剛加的地方
<% end %>

1:47:00

開始講email
guides.rubyonrails.org/action_mailer_basics.html
可以視作 mail 的 controller
問題:
送mail的速度很慢。
deliver_now之類的功能,不然要等信寄出去,會卡住。
一定要用inline css<div style="background:#ccf">寫在tag內</div>

1:52:00

流程問題
用javascript把四個頁面整合成一個頁面

1:55:40

什麼是攻擊
mysql data type text

2:15:00

在講A => B(pay) => B auto POST (JavaScript)(連線中請等待) => A

       |===== 6 try =========> A (背景傳送)

       BASE64是編版格式,不是加密格式
 
over 2 years ago

重點1:
Controller的名字要加sModel的名字不加s
不過,migration的檔案裡面,create_table的時候,Table名稱都會加s。(create_table :users do |t|)
所以,在做relation的時候,就不用加s。(has_many :item)

重點2:
要用Mysql,怎麼安裝
(不過,我的電腦是Darwin,好像不能用apt-getsudo apt-get install mysql-server這好像是Ubuntu的指令)
(參考

  1. 進階開發環境安裝
  2. mac安裝mysql的兩種方法(含配置)
  3. [SQL]在Mac與Ubuntu上安裝MySQL與登入設定
  4. Ubuntu 安裝和設定 MySQL

重點3:
正確的使用git的方法:git init --bare prodject.git
(網路上是查到裸庫

重點4:
merge的正確方法:
要先由branch去合併master
然後再由master去合併branch

重點5:
rails 預設的資料庫是sqlite
在新建新的application的時候要先用-d去把預設的資料庫換成mysql
-d 重新設定資料庫為mysql
rails new project --database=mysql

1:25

Sqlite是一個檔案式的資料庫
很多人進入網站時不能用
一開始就要建好mySQL

2:00

sudo apt-get install mysql-server

apt是一種安裝方式

還需要一個東西,電腦如果沒有

sudo apt-get install libmysqlclient-dev
gem i mysql2

要裝mysql2的gem,必須要有libmysqlclient-dev,
要有libmysqlclient-dev,要先裝mysql-server

4:00

開始講git
ROR TUTORIAL Chapter 1:11. First-time repository setup

4:50

建議方法A:

git init --bare prodject.git

不建議方法B:不能跟別人協同開發 (網路上都教這個,是錯的):

git init project 

不建議方法C:不能跟別人協同開發 (網路上都教這個,是錯的):

git init 

建議方法A:
打ls 之後,會看到一個project.git的資料夾,裡面會有
branches config description HEAD hooks info objects refs等資料夾

不建議方法B跟不建議方法C:
打ls 之後,裡面沒有東西
打ls -al,才會看到.git,不過不能分享給別人
要有project.git才能用git clone這些功能

7:50

把遠端的東西抓下來

git clone

11:00

git status
git add init 加到一個暫存的狀態
git commit -m "註解"
git push origin master

13:00

弄一個project2

git clone /home/imroot/git_temp/project.git/

15:00

git add -A 全部加到一個暫存的狀態
git commit -m
git status 
git push

換到project 2
git pull下來

17:00

衝突的解法

git add -A
git commit
git push   會出現rejected會被中斷掉

解法:git pull
會開始執行auto merge,只要不是同一行就會成功,同一行會失敗
會顯示哪一個檔案merge失敗
到檔案去修改,然後會出現both modified
還是衝突狀態

git add filename之後會變成
modified
這樣就可以commit了
git commit
git push

到projct 2
再執行git pull
就可以了

23:00

git pull之後,如果沒有conflict,就會auto merge成功,就會進到commit的畫面

24:45

講branch

git branch newbranch

小寫d刪除,還沒被merge

git -d branchname 

大寫D,強制刪除

git -D branchname 
git checkout branchname

28:50

merge要先由branch去合併master
然後再由master去合併branch

git checkout brancename
git merge master
auto merge會有conflict,要解conflict之後
git add -A
git commit -m 'fix'
git checkout master
git merge branchname

32:00

git log 可以看過去的所有的commit
git checkout commit_number

34:00

git總整理

git clone init --bare project.git

37:00

開始講rails

rails --help
預設資料庫: sqlite
-d 重新設定資料庫為mysql

rails new project --database=mysql

40:45

project/project$ rails --help 出現的東西會不一樣

41:50

設定mysql的帳號密碼
database.yml (我的現在是sqlite的格式)

yml檔很注意縮排,縮排代表分段

default:&default
  adapter: mysql2
  encoding: utf8
  pool: 5
  username: imroot
  password: imroot
  socket: /var/run/mysqld/mysqls.socket

  production:
   <<: *default
   database: project_production
   username: imroot
   password: imroot

44:00

Gemfile管理版本

46:00

Gemfile.lock這個project裡面所有相依性的gem,要寫到gemfile裡面去之後,才能夠使用。

49:00

開始講所有的檔案
temp README.rdoc public lib Gemfile config.ru bin vendor test Rakefile log Gemfile.lock db config app

53:45

config :所有的設定檔都在這邊,偷別人的就要偷這個
database.yml 資料庫的設定檔

56:00

config/environment
rails分三個模式 development.rb production.rb test.rb

1:00:00

config/secrets.yml
session會被竊取,不要給別人看到

1:03:00

開始實做migration
mysql很重要(我的顯示不出來東西)

mysql -u root -p
如果有一萬筆資料,ruby要用each跑一萬次,mysql只要一行程式

1:08:00

rails g migration init_db
其實只是弄了一個空的檔案,可以打開檔案
把 def change 改成 up(升版,新增) 跟 down (降版,刪除)

1:19:00

def change:
rake db:migrate => 就是up
rake db:rollback => 就是down

1:23:00

最困難的relation開始了

1:25:00

重要的東西放在user table
雜七雜八的放在 profile

1:27:00

一個user只能有一個profile
add_index :profile , [:user_id], :unique => true

1:29:00

資料庫裡面永遠只有一對一

左手拉右手
book_owners
:user_id
:book_id

1:30:00

books
t.string :owner_type
polymorphism
為了跨除一個table只能對一個對象的終極解法

1:31:00

單表繼承 - STI
animal
t.string :type
其他地方不能用type 這個關鍵字

1:35:00

User.rb
has_many :item
建議不要加s。
DB層有s,model層之後都不要有s
ps.
Controller的名字要加s,Model的名字不要加s,
不過,migration的檔案裡面,create_table的時候,Table名稱都會加s。(create_table :users do |t|)
所以,在做relation的時候,就不用加s。(has_many :item)

1:37:00

Profile.rb

1:45:00

user = User.where("name = ?", "Kitty")

1:48:00

user = User.first
profile = Profile.create(:user => user)

因為 User has one Profile
所以放:user => user,Profile自己就會去找到user的id放進:user_id這個column裡面

1:48:00

profile.user
User.find(profile.user.id)

兩個跑出來的結果是一樣的,
可以寫成profile.user主要是因為有寫User has one Profile。
不過就算是沒有寫User has one Profile,也可以打User.find(profile.user.id)找到對應的user.

1:50:00

開始講Book

book = Book.create(:owner => user, :title => "hihi")
owner_type會顯示Class
polymorphism的機制

1:54:00

User has many Book throght Book_Owner
打user.book可以列出book,
是因為mysql的INNER JOIN的語法

1:54:25

不建議“多對多對多”,因為用太多INNER JOIN會拖累速度
這種結構用越少越好
建議用一對多,多對一,或一對一,或快取的方式去拆

1:57:00

polymorphism
ownwer_type: "User"
也可以改成Book,也可以改成Profile
一個東西可以對應到很多table

1:58:00

STI
animal的 type是nil
Cat.create(:name => 'cat')
Cat type:"Cat"

2:12:00

User has one Profile
Profile belongs to User

要同時設定兩邊,才可以由左到右,然後由右到左
user.profile.user
不然只能由一邊到另一邊,不能由另一邊回來一邊

2:13:00

開始講routes
config/routes.rb

先建網址(routes),再建Controller,然後才是View

2:19:00

http的header (get post patch put delete)不一樣,來導到不同的網址

2:21:00

match "/hoo" , :to => "static#test" , :via => [:post, :delete]

限定/hoo這個網址,只有:post跟:delete可以進來

2:31:00

講callback

before_action :hihi

private
def hihi
end

執行這個controller的時候,會先執行hihi這個method,所以其他的method就不用執行一樣的工作。

2:34:00

(.:format)是可以接json那些東西的意思

2:36:00

render跟redirect to

render抓後面的action
redirect_to: 網址會變

2:39:45

views/layouts/application.html.erb

2:42:00

修改完application.html.erb後,再去改其它頁面

<% content_for :head do %>
    hihi i am hea
<% end %>

<% content_for :footer do %>
    hihi i am footer
<% end %>

layout打得好的人,可以塞東西到他想要的地方去

2:45:00

items_controller
def hoo
    render :layout => "dashboard" **(要用string)**
end

進到hoo的頁面的時候,會去抓dashboard的頁面
local:3000/yoo/234

2:46:00

layout 'dashborad' 會有一樣的效果

不同的controller可以抓不同的layout,活性會變很大

2:48:00

html escape
怕被入侵

2:51:00

HELPER

module ApplicationHelper

    def test_me
        return (0..100).to_a.join("|")
    end 

end

到hoo.html.erb
<%= test_me %>
就可以用這個method了。
可以在view使用的程式

2:54:00

_partial
部分的html,不能被action直接用
到html裡面使用
<%= render 'partial' %>
menu啊,logo可以用

2:56:00

示範scaffold

rails g scaffold --help

 
over 2 years ago

http://guides.rubyonrails.org/association_basics.html
Active Record Associations
1) 2.4 The has_many :through Association

A has_many :through association is often used to set up a many-to-many connection with another model. This association indicates that the declaring model can be matched with zero or more instances of another model by proceeding through a third model. For example, consider a medical practice where patients make appointments to see physicians. The relevant association declarations could look like this:

兩個model透過第三方的model來進行聯結。

class Physician < ActiveRecord::Base
  has_many :appointments
  has_many :patients, through: :appointments
end
class Appointment < ActiveRecord::Base
  belongs_to :physician
  belongs_to :patient
end
class Patient < ActiveRecord::Base
  has_many :appointments
  has_many :physicians, through: :appointments
end

class Survey < ActiveRecord::Base
  has_many :categories
  has_many :questions, through: :categories
end
class Category < ActiveRecord::Base
  belongs_to :survey
  belongs_to :question
end
class Question < ActiveRecord::Base
  has_many :categories
  has_many :survey, through: :categories
end

The has_many :through association is also useful for setting up "shortcuts" through nested has_many associations. For example, if a document has many sections, and a section has many paragraphs, you may sometimes want to get a simple collection of all paragraphs in the document. You could set that up this way:
:through可以當作捷徑來使用。

class Document < ActiveRecord::Base
  has_many :sections
  has_many :paragraphs, through: :sections
end
class Section < ActiveRecord::Base
  belongs_to :document
  has_many :paragraphs
end
class Paragraph < ActiveRecord::Base
  belongs_to :section
end

class Survey < ActiveRecord::Base
  has_many :categories
  has_many :questions, through: :categories
end
class Category < ActiveRecord::Base
  belongs_to :survey
  has_many :questions
end
class Question < ActiveRecord::Base
  belongs_to :categories
end

With through: :sections specified, Rails will now understand:
@document.paragraphs
@survey.questions

2) 2.8 Choosing Between has_many :through and has_and_belongs_to_many

The simplest rule of thumb is that you should set up a has_many :through relationship if you need to work with the relationship model as an independent entity. If you don't need to do anything with the relationship model, it may be simpler to set up a has_and_belongs_to_many relationship (though you'll need to remember to create the joining table in the database).

You should use has_many :through if you need validations, callbacks, or extra attributes on the join model.

如果需要
validations,
callbacks,
or extra attributes的話,
應該要用 has_many :through

ihower:Rails還有一種舊式的has_and_belongs_to_many方法也可以建立多對多關係,不過已經很少使用,在此略過不提。

3) 2.9 Polymorphic Associations

A slightly more advanced twist on associations is the polymorphic association. With polymorphic associations, a model can belong to more than one other model, on a single association. For example, you might have a picture model that belongs to either an employee model or a product model. Here's how this could be declared:

一個model可以屬於一個以上的model

class Picture < ActiveRecord::Base
  belongs_to :imageable, polymorphic: true
end
class Employee < ActiveRecord::Base
  has_many :pictures, as: :imageable
end
class Product < ActiveRecord::Base
  has_many :pictures, as: :imageable
end

You can think of a polymorphic belongs_to declaration as setting up an interface that any other model can use. From an instance of the Employee model, you can retrieve a collection of pictures: @employee.pictures.

interface:介面:其他Model可以使用的介面
可以直接下:@employee.pictures 跟 @product.pictures.

class questions < ActiveRecord::Base
  belongs_to :category, polymorphic: true
end
class Survey < ActiveRecord::Base
  has_many :questions, as: :category
end
class Category < ActiveRecord::Base
  has_many :questions, as: :category
end

如果以ihower 的範例來看(https://ihower.tw/rails4/activerecord-relationships.html)
我的案子好像不太適合用polymorphic associations,
我的category應該不太需要更新。

有用的範例:http://www.gotealeaf.com/blog/understanding-polymorphic-associations-in-rails
順便參考一下最下面的:Combining has_many :through and polymorphic association
應用:

一份問卷,有不同的類別,每個類別有許多題目,每個題目有許多選項。

*http://rails101s.logdown.com/posts/211428-active-record-association

代表此變數 ( memebers, participated_groups ) 是經由 ( through ) group_users 這個資料表
來取得 ( user , group ) 資料
(我以為has_and_belongs_to_many才會有group_users這樣的命名方式,原來has_many :through也可以。)

app/models/user.rb
  has_many :group_users
  has_many :participated_groups, :through => :group_users, :source => :group
end
看用戶回答過哪些問題
has_many :answered_questions, :through => :question_users, :source => :question
 
over 2 years ago

複習一下git步驟:(最後一步別忘記)

$ git init
$ git add -A
$ git commit -m "Initialize repository"
create a new repository
$ git remote add origin git@bitbucket.org:<username>/toy_app.git
$ git push -u origin --all # pushes up the repo and its refs for the first time
$ git commit -am "Add hello"
$ heroku create
$ git push heroku master
$ git status
$ git add -A
$ git commit -m "Finish toy app"
$ git push
$ git push heroku
$ heroku run rake db:migrate

摘要:
rails generate scaffold Micropost content:text user_id:integer
bundle exec rake db:migrate
2.3.2:validations是放在model裡面,限制某個column (attribute)的屬性
2.3.3:has_manybelongs_to


2.3.4: Inheritance hierarchies


$ git push heroku之後,記得要$ heroku run rake db:migrate

  1. Now we’re ready to start making the app itself. The typical first step when making a web application is to create a data model, which is a representation of the structures needed by our application.
    In our case, the toy app will be a microblog, with only users and short (micro)posts. Thus, we’ll begin with a model for users of the app (Section 2.1.1), and then we’ll add a model for microposts (Section 2.1.2).

  2. Rails scaffolding is generated by passing the scaffold command to the rails generate script. The argument of the scaffold command is the singular version of the resource name (in this case, User), together with optional parameters for the data model’s attributes:

    $ rails generate scaffold User name:string email:string
    
  3. (Note that there is no need to include a parameter for id; it is created automatically by Rails for use as the primary key in the database.)

  4. To proceed with the toy application, we first need to migrate the database using Rake(Box 2.1):

    $ bundle exec rake db:migrate
    ==  CreateUsers: migrating ====================================================
    -- create_table(:users)
    -> 0.0017s
    ==  CreateUsers: migrated (0.0018s) ===========================================
    This simply updates the database with our new users data model. (We’ll learn more about database migrations starting in Section 6.1.1.) 
    
  5. Note that, in order to ensure that the command uses the version of Rake corresponding to our Gemfile, we need to run rake using bundle exec. On many systems, including the cloud IDE, you can omit bundle exec, but it is necessary on some systems, so I’ll include it for completeness.

  6. 2.2.2 MVC in action

  7. You may notice that there are more actions than there are pages; the index, show, new, and edit actions all correspond to pages from Section 2.2.1, but there are additional create,update, and destroy actions as well. These actions don’t typically render pages (although they can); instead, their main purpose is to modify information about users in the database.

  8. Note fromTable 2.2 that there is some overlap in the URLs; for example, both the user show action and the update action correspond to the URL /users/1. The difference between them is the HTTP request method they respond to.

    HTTP request
    URL
    Action
    Purpose
    GET
    /users
    index
    page to list all users
    GET
    /users/1
    show
    page to show user with id 1
    GET
    /users/new
    new
    page to make a new user
    POST
    /users
    create
    create a new user
    GET
    /users/1/edit
    edit
    page to edit user with id 1
    PATCH
    /users/1
    update
    update user with id 1
    DELETE
    /users/1
    destroy
    delete user with id 1
    Table 2.2: RESTful routes provided by the Users resource in Listing 2.2.
    
  9. This index action has the line @users = User.all (Step 3 in Figure 2.11), which asks the User model to retrieve a list of all the users from the database (Step 4), and then places them in the variable @users (pronounced “at-users”) (Step 5). The User model itself appears inListing 2.6; although it is rather plain, it comes equipped with a large amount of functionality because of inheritance (Section 2.3.4 and Section 4.4). In particular, by using the Rails library called Active Record, the code in Listing 2.6 arranges for User.all to return all the users in the database.

  10. Once the @users variable is defined, the controller calls the view (Step 6), shown inListing 2.7. Variables that start with the @ sign, called instance variables, are automatically available in the views; in this case, the index.html.erb view in Listing 2.7 iterates through the @users list and outputs a line of HTML for each one. (Remember, you aren’t supposed to understand this code right now. It is shown only for purposes of illustration.)

  11. 2.3.3 A user has_many microposts

One of the most powerful features of Rails is the ability to form associations between different data models. In the case of our User model, each user potentially has many microposts. We can express this in code by updating the User and Micropost models as inListing 2.11 and Listing 2.12.
Listing 2.11: A user has many microposts.

app/models/user.rb
class User < ActiveRecord::Base
  has_many :microposts
end

Listing 2.12: A micropost belongs to a user.

app/models/micropost.rb
class Micropost < ActiveRecord::Base
  belongs_to :user
  validates :content, length: { maximum: 140 }
end

We can visualize the result of this association in Figure 2.15. Because of the user_id column in the microposts table, Rails (using Active Record) can infer the microposts associated with each user.

With the completion of the Microposts resource, now is a good time to push the repository up to Bitbucket:

$ git status
$ git add -A
$ git commit -m "Finish toy app"
$ git push
Ordinarily, you should make smaller, more frequent commits, but for the purposes of this chapter a single big commit at the end is fine.

At this point, you can also deploy the toy app to Heroku as in Section 1.5:

$ git push heroku
(This assumes you created the Heroku app in Section 2.1. Otherwise, you should run heroku create and then git push heroku master.)

To get the application’s database to work, you’ll also have to migrate the production database:

$ heroku run rake db:migrate

  1. Inheritance hierarchies

MicropostsController < ApplicationController < ActionController::Base

  1. 2.3.5 Deploying the toy app

With the completion of the Microposts resource, now is a good time to push the repository up to Bitbucket:

$ git status
$ git add -A
$ git commit -m "Finish toy app"
$ git push

Ordinarily, you should make smaller, more frequent commits, but for the purposes of this chapter a single big commit at the end is fine.

At this point, you can also deploy the toy app to Heroku as in Section 1.5:

$ git push heroku

(This assumes you created the Heroku app in Section 2.1. Otherwise, you should run heroku create and then git push heroku master.)

To get the application’s database to work, you’ll also have to migrate the production database:

$ heroku run rake db:migrate
 
over 2 years ago

摘要

1. 作者的教學網站:learnenough.com

2. Gemfile的gem版本設定

3. MVC的介紹圖

4. Git的介紹

5. 如何使用Bitbucket

6. 怎麼找Public key cat ~/.ssh/id_rsa.pub

7. 記得做任何比較大的改變的時候,都先弄一個新的branch

1. a real sample app (Chapter 3 through Chapter 12).

2. gems (self-contained solutions to specific problems such as pagination and image upload)

3. Another strategy recommended by multiple readers is simply to do the tutorial twice; you may be surprised at how much you learned the first time (and how much easier it is the second time through).

灌rails的版本

$ gem install rails -v 4.2.0

用rails指令,新建(new)一個application(hello_app)

$ rails _4.2.0_ new hello_app

4. 1.3.1 Bundler

After creating a new Rails application, the next step is to use Bundler to install and include the gems needed by the app.

5. Unless you specify a version number to the gem command, Bundler will automatically install the latest requested version of the gem. This is the case, for example, in the code gem 'sqlite3'

Gemfile注意事項
gem 'coffee-rails', '~> 4.0.0' ~> 不會超過4.1.0版本
gem 'uglifier', '>= 1.3.0' >= 高過於1.3.0的版本,譬如7.2.0
gem 'sqlite3' 沒有寫任何數字,就是會灌最新的版本

6. Unfortunately, experience shows that even minor point releases can break things, so for the Ruby on Rails Tutorial we’ll err on the side of caution by including exact version numbers for all gems.

7. Converting the Gemfile in Listing 1.4 to use exact gem versions results in the code shown inListing 1.5.

在Gemfile裡面新增任何的gem之後,都要跑一次bundle install

8. 1.3.3 Model-View-Controller (MVC)

This is a hint that Rails follows the model-view-controller (MVC) architectural pattern, which enforces a separation between “domain logic” (also called “business logic”) from the input and presentation logic associated with a graphical user interface (GUI). In the case of web applications, the “domain logic” typically consists of data models for things like users, articles, and products, and the GUI is just a web page in a web browser.

9. 1.4 Version control with Git

Version control systems allow us to track changes to our project’s code, collaborate more easily, and roll back any inadvertent errors (such as accidentally deleting files).

推薦文章:
Bitbucket Getting Started
Pro Git

10. 1.4.1 Installation and setup

Before using Git, you should perform a set of one-time setup steps. These are system setups, meaning you only have to do them once per computer:

這邊的步驟,一台電腦只要設一次:

$ git config --global user.name "Your Name"
$ git config --global user.email your.email@example.com
$ git config --global push.default matching
$ git config --global alias.co checkout

Note that the name and email address you use in your Git configuration will be available in any repositories you make public.
(Only the first two lines above are strictly necessary. The third line is included only to ensure forward-compatibility with an upcoming release of Git.The optional fourth line is included so that you can use co in place of the more verbose checkout command. For maximum compatibility with systems that don’t have co configured, this tutorial will use the full checkout command, but in real life I nearly always use git co.)

11. First-time repository setup

Now we come to some steps that are necessary each time you create a new repository(sometimes called a repo for short). First navigate to the root directory of the first app and initialize a new repository:

首先要新建一個repo

$ git init

ps 這邊跟JC大講解的不一樣:RailsFun.tw 新手教學_day2 HD
4:50 JC大:應該用 'git init --bare prodject.git'

Initialized empty Git repository in /home/ubuntu/workspace/hello_app/.git/

The next step is to add all the project files to the repository using git add -A:

把所有的檔案都夾到repo裡面去

$ git add -A

實際上,都還留在staging area,然後要commit跟給附註(-m)才能夠上傳。

This command adds all the files in the current directory apart from those that match the patterns in a special file called .gitignore. The rails new command automatically generates a .gitignore file appropriate to a Rails project, but you can add additional patterns as well.12

The added files are initially placed in a staging area, which contains pending changes to your project. You can see which files are in the staging area using the status command:

用git status來看有什麼處在pending的改變

$ git status
On branch master

Initial commit

Changes to be committed:
  (use "git rm --cached <file>..." to unstage)

  new file:   .gitignore
  new file:   Gemfile
  new file:   Gemfile.lock
  new file:   README.rdoc
  new file:   Rakefile
  .
  .
  .

To tell Git you want to keep the changes, use the commit command:
** -m 是用來寫附註的:**

$ git commit -m "Initialize repository"
[master (root-commit) df0a62f] Initialize repository
.
.
.

The -m flag lets you add a message for the commit; if you omit -m, Git will open the system’s default editor and have you enter the message there. (All the examples in this book will use the -m flag.)

其實這個時候,還留在local端,要用'git push'才會上傳
It is important to note that Git commits are local, recorded only on the machine on which the commits occur. We’ll see how to push the changes up to a remote repository (using git push) in Section 1.4.4.

By the way, you can see a list of your commit messages using the log command:

$ git log
commit df0a62f3f091e53ffa799309b3e32c27b0b38eb4
Author: Michael Hartl <michael@michaelhartl.com>
Date:   Wed August 20 19:44:43 2014 +0000

    Initialize repository
Depending on the length of your repository’s log history, you may have to type q to quit.

Let’s check the status to see what changed:

$ git status

12. 1.4.3 Bitbucket

Once you’ve added your public key, click on “Create” to create a new repository, as shown inFigure 1.14.
When filling in the information for the project, take care to leave the box next to “This is a private repository.” checked.
After clicking “Create repository”, follow the instructions under “Command line > I have an existing project”, which should look something like Listing 1.12. (If it doesn’t look like Listing 1.12, it might be because the public key didn’t get added correctly, in which case I suggest trying that step again.)
When pushing up the repository, answer yes if you see the question “Are you sure you want to continue connecting (yes/no)?”

how to install a public key on your Bitbucket account.

Public Key

Listing 1.11: Printing the public key using cat.
$ cat ~/.ssh/id_rsa.pub
Listing 1.12: Adding Bitbucket and pushing up the repository.
$ git remote add origin git@bitbucket.org:<username>/hello_app.git
$ git push -u origin --all # pushes up the repo and its refs for the first time

The commands in Listing 1.12 first tell Git that you want to add Bitbucket as the origin for your repository, and then push your repository up to the remote origin. (Don’t worry about what the -u flag does; if you’re curious, do a web search for “git set upstream”.) Of course, you should replace with your actual username. For example, the command I ran was

$ git remote add origin git@bitbucket.org:mhartl/hello_app.git

13. Branch

Git is incredibly good at making branches, which are effectively copies of a repository where we can make (possibly experimental) changes without modifying the parent files. In most cases, the parent repository is the master branch, and we can create a new topic branch by using checkout with the -b flag:

checkout -b可以新增一個new branch,同時switch去這個new branch

$ git checkout -b modify-README

Switched to a new branch 'modify-README'
$ git branch
  master
* modify-README

Here the second command, git branch, just lists all the local branches, and the asterisk *identifies which branch we’re currently on. Note that git checkout -b modify-README both creates a new branch and switches to it, as indicated by the asterisk in front of th emodify-README branch. (If you set up the co alias in Section 1.4, you can use git co -b modify-README instead.)

git commit provides the -a flag as a shortcut for the (very common) case of committing all modifications to existing files (or files created using git mv, which don’t count as new files to Git):

$ git commit -a -m "Improve the README fil

Be careful about using the -a flag improperly; if you have added any new files to the project since the last commit, you still have to tell Git about them using git add -A first.

如果只是一些小改變,可以直接用git commit -a -m,可是如果有新增檔案,就必須先用git add -A然後再用git commit -m

14 Merge

Now that we’ve finished making our changes, we’re ready to merge the results back into our master branch:

$ git checkout master
Switched to branch 'master'
$ git merge modify-README
Updating 34f06b7..2c92bef
Fast forward
README.rdoc     |  243 --------------------------------------------------
README.md       |    5 +
2 files changed, 5 insertions(+), 243 deletions(-)
delete mode 100644 README.rdoc
create mode 100644 README.md
After you’ve merged in the changes, you can tidy up your branches by deleting the topic branch using git branch -d if you’re done with it:
$ git branch -d modify-README
Deleted branch modify-README (was 2c92bef).

這邊跟JC講的不一樣:RailsFun.tw 新手教學_day2 HD

28:50

merge要先由branch去合併master
然後再由master去合併branch

通常很少把分支刪掉
This step is optional, and in fact it’s quite common to leave the topic branch intact. This way you can switch back and forth between the topic and master branches, merging in changes every time you reach a natural stopping point.

As mentioned above, it’s also possible to abandon your topic branch changes, in this case with git branch -D:
# For illustration only; don't do this unless you mess up a branch
$ git checkout -b topic-branch
$ <really screw up the branch>
$ git add -A
$ git commit -a -m "Major screw up"
$ git checkout master
$ git branch -D topic-branch

Unlike the -d flag, the -D flag will delete the branch even though we haven’t merged in the changes.
-D是可以不用merge 就可以暴力把branch刪掉

15 Push

Now that we’ve updated the README, we can push the changes up to Bitbucket to see the result. Since we have already done one push (Section 1.4.3), on most systems we can omit origin master, and simply run git push:

$ git push

1.5 Deploying - Heroku

Heroku 使用 PostgreSQL database

Heroku uses the PostgreSQL database (pronounced “post-gres-cue-ell”, and often called “Postgres” for short), which means that we need to add the pg gem in the production environment to allow Rails to talk to Postgres:17

:production 要用pg (PostgreSQL)

group :production do
  gem 'pg',             '0.17.1'
  gem 'rails_12factor', '0.0.2'
end

Note also the addition of the rails_12factor gem, which is used by Heroku to serve static assets such as images and stylesheets. Finally, be sure to incorporate the changes made in Listing 1.5 preventing the sqlite3 gem from being included in a production environment, since SQLite isn’t supported at Heroku:

:development要用sqlite3

group :development, :test do
  gem 'sqlite3',     '1.3.9'
  gem 'byebug',      '3.4.0'
  gem 'web-console', '2.0.0.beta3'
  gem 'spring',      '1.1.3'
end

雖然加上without production,不會在local端install pg跟rails_12factor這兩個gem,不過其實是更新到Gemfile.lock

$ bundle install --without production

Because the only gems added in Listing 1.14 are restricted to a production environment, right now this command doesn’t actually install any additional local gems, but it’s needed to update Gemfile.lock with the pg and rails_12factor gems.

再把剛剛做的事情在commit一下

$ git commit -a -m "Update Gemfile.lock for Heroku"

1.5.1 Heroku setup

確定有沒有灌過heroku CLI跟Heroku Toolbelt

$ heroku version

登入跟輸入SSH key
Once you’ve verified that the Heroku command-line interface is installed, use the heroku command to log in and add your SSH key:

$ heroku login
$ heroku keys:add

Finally, use the heroku create command to create a place on the Heroku servers for the sample app to live (Listing 1.15).

新建一個網址

$ heroku create

在push到heroku master,就好了

$ git push heroku master
 
over 2 years ago

RailsFun_新手教學_day1 _HD

01:30

rvm.io

07:25

教method怎麼看。
ruby doc
ruby float doc
左邊是如何使用的列表

09:00

.method

18:00

"yo\noo" => "yo\noo"
'yo\noo' => yo   noo

29:00

布林運算

三元運算式:正確執行第一個,錯誤執行第二個

false ? 123 : 234
=> 234
錯誤,第二個

true ? 123 : 456
=> 123
正確,第一個

40:00

method 的用法

def temp1
    puts 123
end
def temp(x)
    puts x
end
temp(100) => 100

括號可以省略,可以做縮寫

def temp x
    puts x
end
temp(100) => 100

42:30

wrong number of argument  (2 for 3)

要三個變數,可是只給兩個

44:00

有預設值可以不用給

temp (x , y=100, z = 10)

44:30

俗稱的變數名稱:argv,其實也可以隨便改,譬如list

def temp (x , y=100, *argv)
  ap [x , y , argv]
end
temp(1)
=> 
[0] 1
[1] 100
[2] []
temp(1,2)
=> 
[0] 1
[1] 2
[2] []
temp(1,2, 3,4,5,6,7,8)
=> 
[0] 1
[1] 2
[2] [
    [0] 3
    [1] 4
    [2] 5
    [3] 6
    [4] 7
    [5] 8
]

46:00 很難懂的&block,其實也是個名稱,可以改為&werwer

def temp (x , y=100, *argv, &block)
  block.call(x , y, argv)
end
temp(1,2,3) do |x,y,argv|
  ap [x,y,argv]
end

=> 
[0] 1
[1] 2
[2] [
    [0] 3
]

49:00

&block是一支程式

=> do |x,y,argv|
  ap [x,y,argv]
end

51:30

講PROC

Proc.new do |x| puts x ; end
=>
Proc.new do |x|
    puts x
end

=> Proc.new { |x|
    puts x
}

[1,2,3,4].each {|i| puts i}

53:00

x = Proc.new {|x| puts x}
x.call(999)
=>999

54:00

def temp(&block)
    puts block.class
    block.call(99)
end

temp {|x| puts x}
Proc
99

56:00

最後一個參數是hash (這是method的使用,前面講的都是method的定義)

1:04:00

變數的生存範圍

1:05:00

class Car
    def self.hihi #工廠在用

    puts 'yoo'
    end
    def hihi #汽車在用
        put 'hoo'
    end
end

1:10:00

class Car #這邊要用@@

    def self.set_name(name)
        @@name = name
    end
    def self.get_name
        @@name
    end
end

1:17:00

@XXX ||= N

1:18:00

@moo ||= 123 如果沒有,就給他123,如果有的話,就不動它。
@moo = 123 unless @moo

如果@moo是false (只有nil跟false是false),unless @moo會變true,就會執行@moo=123
如果@moo有值(譬如是456),就會變成true(有值就會是true),unless @moo 就會變false,就不會執行@moo=123,會維持原有的@mooo (也就456)

非常適合放在method裡面,尤其是接到某個值之後,要去做一些初始化的動作的時候,就可以這樣寫。因為call一個method通常會塞一些東西進去,然後要做一些處理,勢必要去判斷那個東西的時候,這樣寫會很方便。

1:20:00

def temp option = nil
    option ||= {:name => "Kitty"}
    return
end

temp 
=> {:name => "Kitty"}

temp :wer =>234 
=> {:wer =>234}

1:25:40

開始講module
Module沒有new
跟工廠的用法一樣,不需要new的東西可以用module,不需要生出小汽車的東西可以直接用module

module Muu
    def self.temp
        puts 'temp'
    end
end

Muu.temp => temp

1:27

講self

1:30:00

講super:call老爸,被蓋過的method可以call super再去用老爸

1:32:50

講mixin

module A
    def hello
        puts "Kitty"
    end
end

不能夠直接A.hello

要用

Class B
    include A
end

B.new.hello => "Kitty"

1:40:00

開始看ruby-doc.org

1:55:00

開始講hash

.each do  | array[key,value]|
.each_pair do |key, value|
.each_value do |value|
.each_key do |key|

中間再講regex

2:17:00

講marshal : 可以把所有東西都變成string

2:23:00

講thread

2:38:00 open-uri(或是使用curb)跟okogiri的範例

開始講nokogiri:分析HTML

$irb

terminal 先require這兩個功能
irb(main):require 'open-uri'
irb(main):require 'nokogiri'
terminal 先用open-uri的read
irb(main):open('http://www.tmuh.org.tw/team/team/053/053/050216')
=> #<Tempfile:/var/folders/18/7jxtd5l14sj9jqqqnqjjwzs80000gn/T/open-uri20170101-49023-eh0exw>

irb(main):003:0> open('http://www.tmuh.org.tw/team/team/053/053/050216').read
terminal 使用Nokogiri::HTML()
irb(main):html = open('http://www.tmuh.org.tw/team/team/053/053/050216').read
irb(main):doc = Nokogiri::HTML(html)
irb(main):doc.search('img')
terminal
irb(main):
doc.search('img').each do |img|
    puts img.attr('src')
end ; true
terminal 如果沒有http://的話就是相對路徑,就要先map拿出來,再塞回去。
ans = []
doc.search('img').each do |img|
    ans << img.attr('src')
end ; true
temp_ans = ans.map{|url| url.match(/^htpp/) ? url : "http://www.tmuh.org.tw#{url}"}
terminal 叫出來看:
puts temp_ans
terminal 如何Linux下,抓一張圖
temp_ans.each do |full_url|
`wget #{full_url}`
end
 
over 2 years ago

Hi, This a demo post of Logdown.

Logdown use Markdown as main syntax, you can find more example by reading this document on Wikipedia

Logdown also support drag & drop image uploading. The picture syntax is like this:

Bloging with code snippet:

inline code

Plain Code

puts "Hello World!"

Code with Language

puts "Hello World!"

Code with Title

hello_world.rb
puts "Hello World!"

MathJax Example

Mathjax

Inline Mathjax

The answser is .

Table Example

Tables Are Cool
col 1 Hello $1600
col 2 Hello $12
col 3 Hello $1