GoRails

1.Environment Variables<b>[finish]</b>
2.Ruby Version Managers<b>[finish]</b>
3.Rails Application Structure<b>[finish]</b>
4.The Params Hash<b>[finish]</b>
5.Sending Data Between Controllers And Views<b>[finish]</b>
6.Querying Named Scopes Across Models with ActiveRecord::Relation#merge<b>[finish]</b>
7.Styling with Bootstrap Sass <b>[finish]</b>
8.Debugging With BetterErrors<b>[finish]</b>
9.Pretty urls with FriendlyID<b>[finish]</b>
10.Pagination with will_paginate<b>[finish]</b>
11.File Uploading with Carrierwave<b>[finish]</b>
12.User Authentication with Devise<b>[finish]</b>
13.Keeping track with Annotate<b>[finish]</b>
14.Sending emails with Mandrill
15.Understanding Semantic Versioning<b>[finish]</b>
16.Button Loading Animations with jQuery UJS<b>[finish]</b>
17.jQuery UJS and AJAX<b>[finish]</b>
18.jQuery UJS Callbacks
19.A Look Into Routing<b>[finish]</b>
20.Authorization With CanCanCan
21.Rails 4.2 Introduction <b>[finish]</b>
22.Authorization with Pundit
23.Consuming an API Using HTTParty and Creating a Gem<b>[finish]</b>
24.Liking Posts
25.Forum Series Part 1: Architecture<b>[finish]</b>
26.Forum Series Part 2: Routes<b>[finish]</b>
27.Forum Series Part 3: Nested Attributes and fields_for<b>[finish]</b>
28.Forum Series Part 4: div_for
29.Forum Series Part 5: Email Notifications with Rails 4.2, deliver_later, and Previews
30.Forum Series Part 6: Search with Ransack
31.Forum Series Part 7: Time Zones using the local_time Gem
32.Markdown and Emoji with the html-pipeline gem
33.Sign In With Twitter using Omniauth and the Twitter gem<b>[finish]</b>
34.Forms With Multiple Submit Buttons
35.API Tokens with Devise Token Authenticatable
36.Comments With Polymorphic Associations
37.File Uploads with Refile
38.Upload Progress with Refile Javascript
39.Refactoring Controller Methods
40.Fragment Caching And oEmbed
41.Soft Delete with Paranoia<b>[finish]</b>
42.Concerns: Making a Soft Deletable module
43.Introduction to Importing from CSV
44.CSV Upload Form to Import Records
45.Exporting Records To CSV
46.Refactoring CSV Uploads with ActiveModel::Model
47.Multitenancy with the Apartment gem
48.Handling Subdomains and Multitenancy From Scratch
49.Newsletter Sign Up Form with country_select
50.Using Ruby Service Objects To Refactor Your Rails Code
51.PDF Receipts
52.Refactoring with the Null Object Pattern
53.How To Setup Vagrant For Rails Development<b>[finish]</b>
54.Debugging Performance Problems In Your Views
55.My Development Environment
56.Refactoring Your jQuery Code with Objects in Coffeescript
57.Including Javascript and CSS Libraries With Rails
58.Clean Javascript Code Using Data-Behavior On The Frontend
59.How To Create A Gem For Frontend Javascript And CSS Libraries
60.Basic Authentication and RSS Feeds
61.Manage Assets With Rails Assets<b>[finish]</b>
62.Open Source Vlog - Where Do We Even Start?
63.Open Source Vlog - Housekeeping and Merging Pull Requests
64.Open Source Vlog - Setting Up Rspec
65.Open Source Vlog - Setting Up For The Future
66.Open Source Vlog - Just Deleting Things
67.Open Source Vlog - Building A Calendar From Scratch
68.Open Source Vlog - Refactoring Our Calendar
69.Open Source Vlog - Filtering Events
70.Activity Feed From Scratch
71.Pair Programming on Rails Scopes
72.Open Source Vlog - Finding a balance between Ruby and ERB
73.Open Source Vlog - Simple And Clean RubyGem With Rails Engines
74.Elegant Refactoring for Week and Month Calendars
75.Rails 5's ActionCable and Websockets Introduction
76.Open Source Vlog - Refactoring Events And Uploading Version 2.0
77.Open Source Vlog - Rails Generators
78.@Mentions, Autocomplete, Notifications, and Links
79.Open Source Vlog - Sorting Calendar Events
80.Redirect To Current Page After Login
81.Open Source Vlog - Writing Rspec Tests
82.Usability And Video Autoplay With Wistia
83.Using Purchased Themes with Rails
84.What Is A State Machine? <b>[finish]</b>
85.The State_Machine Gem<b>[finish]</b>
86.Scheduling Posts
87.Scheduled Cron Jobs with the Whenever Gem
88.Using Bootstrap 4 Rubygem with Rails<b>[finish]</b>
89.Advanced Search, Autocomplete and Suggestions with ElasticSearch and the Searckick gem
90.Subscriptions with Stripe
91.Adding Stripe update card form, Cancel, and Resubscribe
92.Contributing To Open Source: Fixing A Bug In Devise
93.Using Webhooks with Stripe
94.Admin Interfaces with Administrate
95.Background Workers with ActiveJob and Sidekiq
96.In-App Navbar Notifications
97.Integrating Braintree (and PayPal)
98.Virtual Attributes And Rails 5 Attribute API
99.Metaprogramming Virtual Attributes
100.Sharing Data With Javascript
101.ShareMeow
102.Deploying To Production on Heroku with Puma
103.How to Deploy ActionCable and Rails 5 To Production
104.Deploying Sidekiq To Heroku
105.In-App Messages Between Users
106.Mitigating Spammers with Recaptcha
107.How to Upgrade to Turbolinks 5
108.Advanced Messaging with Mailboxer: Mark As Read
109.Advanced Mailboxer: Multiple User Conversations
110.Advanced Messaging with Mailboxer: Adding Attachments
111.Memoization
112.Upgrading simple_calendar for Rails 5
113.GoRails Performance - The Techniques I Use
114.Russian Doll Caching with Rails 5
115.Advanced Caching with User Permissions and Authorization
116.Turbolinks 5 Forms for Mobile
117.Sentiment Analysis with the Sentimental Gem
118.Repost / Retweet / Reblog
119.Improving Query Performance with Database Indexes
120.Inviting Users with devise_invitable
121.Custom will_paginate Methods
122.Message Templates
123.Wrapping Business Logic with Plain Old Ruby Objects
124.Improving In-App Notifications
125.Realtime Notifications with ActionCable
126.How ActionCable Uses Redis
127.Solving FizzBuzz in Ruby
128.Disabling ActionCable for Unauthenticated Users
129.Group Chat with ActionCable: Part 1
130.Group Chat with ActionCable: Part 2
131.Group Chat with ActionCable: Part 3
132.Group Chat with ActionCable: Part 4
133.Group Chat with ActionCable: Part 5
134.Sending Emails with SMTP and Sendgrid
135.Group Chat with ActionCable: Part 6
136.Group Chat with ActionCable: Part 7
137.Multi-User Spreadsheets with ActionCable: Part 1
138.Multi-User Spreadsheets with ActionCable: Part 2
139.Multi-User Spreadsheets with ActionCable: Part 3
140.Multi-User Spreadsheets with ActionCable: Part 4
141.Multi-User Spreadsheets with ActionCable: Part 5
142.File Uploads in Rails With Shrine
143.Direct Messages in Realtime with ActionCable
144.Direct File Uploads to S3: Part 1
145.Direct File Uploads to S3: Part 2
146.Direct File Uploads to S3: Part 3
147.Electronic Signatures with the Docusign API
148.Receiving Webhooks in Development with Ngrok and Docusign
149.Protecting from XSS with Sanitize

1.Environment Variables

一個(gè)常見(jiàn)的場(chǎng)景是要隱藏?cái)?shù)據(jù)庫(kù)密碼的變量拳恋,下面解決幾種方式:
其中數(shù)據(jù)庫(kù)密碼的格式應(yīng)該如下所示:

#database.yml
password: <%= ENV["PASSWORD"]%> #必須是erb的形式须误,源碼是通過(guò)erb進(jìn)行解析的

1恰聘、臨時(shí)性的解決方法

#PASSWORD=password rails c
上面的方法只是對(duì)本次啟動(dòng)有效义郑,屬于臨時(shí)性解決方案

2姆蘸、將環(huán)境變量存在本地

#ubuntu
ubuntu啟動(dòng)bash的過(guò)程中怨喘,會(huì)啟動(dòng)~/.bash_profile文件厅贪,可以在文件中輸入如下代碼:
export PASSWORD=root  #.bash_profile文件需要重啟或者當(dāng)下執(zhí)行source .bash_profile才能生效

#mac
mac已經(jīng)安裝了zsh,不會(huì)自動(dòng)觸發(fā).bash_profile文件,可以在.zshrc文件添加如下代碼:
source .bash_profile

測(cè)試如上的代碼是否成功伺通,可以通過(guò)終端執(zhí)行env代碼查看

3箍土、在開(kāi)發(fā)環(huán)境中可以通過(guò)dotenv

1、gem 'dotenv-rails'
2罐监、項(xiàng)目文件中(第一層級(jí))建立.env文件吴藻,按照類似如下格式填寫(xiě)
S3_BUCKET=YOURS3BUCKET

參考視頻
4、在生產(chǎn)環(huán)境中使用Figaro
參考視頻

2.Ruby Version Managers

該視頻講述了ruby的版本管理工具弓柱,包括rvm和rbenv,但是目前對(duì)目前rvm如何管理各種gem或者說(shuō)各種版本的rails如何操作不清楚沟堡。

3.Rails Application Structure

這個(gè)視頻只是介紹rails的結(jié)構(gòu)侧但,但是在行文中經(jīng)常出現(xiàn)如下的代碼

require ::File.expand_path('../config/environment', __FILE__)

下面分別對(duì)expand_path和require方法進(jìn)行解釋和理解。

expand_path的解釋和理解:
expand_path是將路徑名轉(zhuǎn)變?yōu)榻^對(duì)路徑名航罗,下面介紹幾種類型

#1.出現(xiàn)用戶目錄“~”禀横,擴(kuò)展用戶目錄成為絕對(duì)路徑
File.expand_path("~oracle/bin") #=> "/home/oracle/bin"

#2.出現(xiàn)文件名和路徑名稱,將兩者結(jié)合
File.expand_path("ruby", "/usr/bin") #=> "/usr/bin/ruby"

#3.更為常見(jiàn)的是這種形式粥血,出現(xiàn)__FILE__柏锄,其中這個(gè)變量的意為所處文件的文件名,下面代碼中:
File.expand_path("../../lib/mygem.rb", __FILE__)
#=> ".../path/to/project/lib/mygem.rb"
其中__FILE__表示為mygem.rb复亏,該文件的父目錄為project,父目錄的父目錄為to,該to文件夾的絕對(duì)目錄為.../path/趾娃,進(jìn)行補(bǔ)全操作,示例所示蜓耻。

require的用法和理解,并且對(duì)比require_relative,load,include,extend械巡。
按照api的解釋刹淌,require的文件名應(yīng)該是絕對(duì)路徑,按照上面的出現(xiàn)的所示讥耗,其中“::”表示回到root路徑中有勾,如果require的是相對(duì)路徑,那么require的文件需要到$LOAD_PATH(又名$:)中尋找古程,方式如下:

irb:$LOAD_PATH

["/Users/jayzen/.rvm/gems/ruby-2.3.0@global/gems/did_you_mean-1.0.0/lib", "/Users/jayzen/.rvm/rubies/ruby-2.3.0/lib/ruby/site_ruby/2.3.0", "/Users/jayzen/.rvm/rubies/ruby-2.3.0/lib/ruby/site_ruby/2.3.0/x86_64-darwin15", "/Users/jayzen/.rvm/rubies/ruby-2.3.0/lib/ruby/site_ruby", "/Users/jayzen/.rvm/rubies/ruby-2.3.0/lib/ruby/vendor_ruby/2.3.0", "/Users/jayzen/.rvm/rubies/ruby-2.3.0/lib/ruby/vendor_ruby/2.3.0/x86_64-darwin15", "/Users/jayzen/.rvm/rubies/ruby-2.3.0/lib/ruby/vendor_ruby", "/Users/jayzen/.rvm/rubies/ruby-2.3.0/lib/ruby/2.3.0", "/Users/jayzen/.rvm/rubies/ruby-2.3.0/lib/ruby/2.3.0/x86_64-darwin15"]

但是目前的ruby版本中已經(jīng)將當(dāng)前路徑從$LOAD_PATH中刪除了蔼卡,因此下面的方式是會(huì)出來(lái)LoadError錯(cuò)誤的:

#demo.rb
require  "test" #test.rb和demo.rb處于同一個(gè)文件件中

如果要采取加載同一個(gè)文件夾中的路徑,需要使用如下的代碼:

#demo.rb
require "./test"
require_relative "test"

load和require的區(qū)別如下:

1. 使用require文件的時(shí)候可以不使用后綴挣磨,load需要
2. require只會(huì)加載一次雇逞,第一次成功加載,返回true,第二次加載茁裙,返回false
3. load可以進(jìn)行反復(fù)多次加載

對(duì)比include,extend與require和load的區(qū)別:

1. include和extend與require,load的區(qū)別在于塘砸,前兩者其實(shí)在同一個(gè)文件中引入模名,后兩者是在不同的文件中引入文件名晤锥。
2. 類中include模塊是引入模塊中的實(shí)例方法作為類的實(shí)例方法使用掉蔬,類中extend模塊是引入模塊中的實(shí)例方法作為類的類方法使用。
4.The Params Hash

視頻講述了生成params值的三種方式矾瘾,guides中都有講述女轿,這邊做一個(gè)小結(jié)。

#通過(guò)url
http://localhost:3000/posts?page=2  #params[:page]=2

#通過(guò)設(shè)置routes
get "blog/:id", to: "blog#show"  #params[:id]=2

#通過(guò)view中的form_tag和form_for
其中form_tag壕翩,直接獲取屬性的值蛉迹,而form_for獲得一個(gè)嵌套參數(shù)
5.Sending Data Between Controllers And Views

開(kāi)發(fā)的過(guò)程中,一個(gè)顯著的現(xiàn)象是controller中的實(shí)例變量才能被傳遞到對(duì)應(yīng)的view中放妈,而局部變量則不能被傳遞婿禽,如何解釋這種現(xiàn)象赏僧,視頻中的材料因?yàn)闀簳r(shí)沒(méi)有找到對(duì)應(yīng)的源碼解讀,因此在這里自己做一些理解和整理扭倾。
在元編程中淀零,對(duì)象是一組實(shí)例變量和一個(gè)指向類的引用組成的。具體的實(shí)例代碼如下:

class Demo
  def test
    @test = "this is the test method in class demo"
  end
end

obj = Demo.new
obj.test #只有方法被調(diào)用膛壹,實(shí)例變量@test才能出現(xiàn)在實(shí)例obj中
puts obj.instance_variables #=>[:@test]

因此我自己的理解是驾中,對(duì)于controller的action所render的對(duì)象是一個(gè)controller的實(shí)例化對(duì)象,對(duì)象能夠訪問(wèn)對(duì)象內(nèi)部的實(shí)例變量模聋,而對(duì)于局部變量肩民,出了局部作用域,該變量就無(wú)效了链方。

6.Querying Named Scopes Across Models with ActiveRecord::Relation#merge

這邊介紹集中ActiveRecord的一些查詢方法持痰,指出joins和left_joins(left_outer_joins)的區(qū)別。

#存在兩個(gè)model,分別是user和post,其中user has many posts
User.joins(:posts) #必須滿足下面語(yǔ)句中的on條件祟蚀,才能夠返回結(jié)果
# SELECT "users".*
# FROM "users"
# INNER JOIN "posts" ON "posts"."user_id" = "users"."id"

User.left_joins(:posts) #如果右邊沒(méi)有滿足條件的實(shí)例工窍,會(huì)返回全部左邊的實(shí)例
User.left_outer_joins(:posts) #和上面的效果是一致的
# SELECT "users".*
# FROM "users"
# INNER JOIN "posts" ON "posts"."user_id" = "users"."id"

本視頻主要講述,在model中的scope過(guò)程中前酿,merge方法有利于簡(jiǎn)化model的查詢語(yǔ)句患雏。語(yǔ)句代碼如下:

#author.rb
class Author < ActiveRecord::Base
  has_many :books
end

#book
class Book < ActiveRecord::Base
  belongs_to :author
  scope :available, ->{ where(available: true) }
end

#沒(méi)有merge情況下:
Author.joins(:books).where("books.available = ?", true) #成功
Author.joins(:books).available #報(bào)錯(cuò),因?yàn)閍vailable是Book類的方法

#存在merge的情況下:
Author.joins(:books).merge(Book.available) #成功罢维,和上面的效果一致淹仑,而且表現(xiàn)的簡(jiǎn)潔

#SQL語(yǔ)法如下所示
SELECT "authors".* FROM "authors" INNER JOIN "books" ON "books"."author_id" = "authors"."id" WHERE "books"."available" = 't'
7.Styling with Bootstrap Sass

在自己筆記的協(xié)同環(huán)境和相關(guān)配置中有介紹。

8.Debugging With BetterErrors

改變默認(rèn)的出錯(cuò)頁(yè)面肺孵,并且可以在頁(yè)面上進(jìn)行debug匀借,注意必須在development而不是在production中,因此gem的安裝必須按照如下所示形式:

#gemfile
group :development do 
  gem "better_errors"
  gem 'binding_of_caller'
end
9.Pretty urls with FriendlyID

相關(guān)的內(nèi)容已經(jīng)在railscasts中進(jìn)行了說(shuō)明平窘,視頻中的不足在于對(duì)于history也沒(méi)有進(jìn)行操作怀吻,自己針對(duì)history的內(nèi)容沒(méi)有執(zhí)行成功初婆。

10.Pagination with will_paginate

已經(jīng)在railscasts總結(jié)中給出詳細(xì)描述蓬坡。

11.File Uploading with Carrierwave

關(guān)于使用Carrierwave來(lái)進(jìn)行文件上傳的已經(jīng)在railscasts總結(jié)中給出。這里使用rails自帶的方法來(lái)上傳附件磅叛。

#模型
rails g scaffold book name cover_filename

#model數(shù)據(jù)呈現(xiàn)
class Book < ApplicationRecord
  attr_accessor :cover

  after_save :save_cover_image, if: :cover

  def save_cover_image
    filename = cover.original_filename
    folder   = "public/books/#{id}/cover"

    FileUtils.mkdir_p folder

    f = File.open File.join(folder, filename), "wb"
    f.write cover.read()
    f.close

    self.cover = nil
    update cover_filename: filename
  end
end

#view數(shù)據(jù)呈現(xiàn)
#_form.html.erb
<%= f.file_field :cover %>

#show.html.erb
<%= image_tag "/books/#{@book.id}/cover/#
12.User Authentication with Devise

在ruby on rails tutorial中有成套的建立用戶登錄的機(jī)制屑咳,但是如果寫(xiě)出來(lái)的會(huì)很話時(shí)間,這里可以使用devise這個(gè)gem作為用戶的驗(yàn)證弊琴,這里記住一個(gè)是:
1兆龙、假定model是user的情況下,熟悉devise的一些基本路由和由此設(shè)定的方法。
2紫皇、假定user要發(fā)布一篇文章post慰安,user和post需要進(jìn)行關(guān)聯(lián),熟悉關(guān)聯(lián)過(guò)程中的一些filter方法聪铺。
3化焕、在最開(kāi)始使用devise過(guò)程中的基本設(shè)置方法。
下面的代碼基本上是基于如上的一些設(shè)置展開(kāi)的铃剔。

#安裝devise
gem 'devise' #gemfile中添加devise
rails g devise:install #終端中輸入代碼顯示安裝devise,會(huì)出現(xiàn)上面的提示
rails g devise:views #會(huì)顯示出登錄界面撒桨,默認(rèn)有界面,但是只有執(zhí)行這段代碼才能找到修改界面
rails g devise user #使用user作為devise的model

#前端提示顯示
<p class="notice"><%= notice %><p>
<p class="alert"><%= alert %></p>

在上面的基礎(chǔ)上執(zhí)行rails routes

                  Prefix Verb   URI Pattern                    Controller#Action
        new_user_session GET    /users/sign_in(.:format)       devise/sessions#new
            user_session POST   /users/sign_in(.:format)       devise/sessions#create
    destroy_user_session DELETE /users/sign_out(.:format)      devise/sessions#destroy
           user_password POST   /users/password(.:format)      devise/passwords#create
       new_user_password GET    /users/password/new(.:format)  devise/passwords#new
      edit_user_password GET    /users/password/edit(.:format) devise/passwords#edit
                         PATCH  /users/password(.:format)      devise/passwords#update
                         PUT    /users/password(.:format)      devise/passwords#update
cancel_user_registration GET    /users/cancel(.:format)        devise/registrations#cancel
       user_registration POST   /users(.:format)               devise/registrations#create
   new_user_registration GET    /users/sign_up(.:format)       devise/registrations#new
  edit_user_registration GET    /users/edit(.:format)          devise/registrations#edit
                         PATCH  /users(.:format)               devise/registrations#update
                         PUT    /users(.:format)               devise/registrations#update
                         DELETE /users(.:format)               devise/registrations#destroy

因此可以使用下面的方法放在view或者controller中

new_user_registration_path #用戶注冊(cè)u(píng)rl
new_user_session_path #用戶登錄url
destroy_user_session_path #用戶退出url
edit_user_registration_path #對(duì)登錄的用戶進(jìn)行編輯

user_signed_in? #判斷用戶是否登錄
current_user #當(dāng)前用戶

authenticate_user! #在controller中的before_action中的過(guò)濾方法,在關(guān)聯(lián)的模型的controller中使用

在model中建立如下devide(user)擴(kuò)展了如下的模塊:

class User < ApplicationRecord
 # Include default devise modules. Others available are:
 # :confirmable, :lockable, :timeoutable and :omniauthable
  devise :database_authenticatable, :registerable, :recoverable, :rememberable, :trackable, :validatable
end
#比如在博客中键兜,如果不需要注冊(cè)凤类,需要將:registerable模塊取消
#建立博客的原始用戶方式是在終端中輸入如下的片段
#rails c 
User.create(email: "xx", password: "111111")

根據(jù)是否通過(guò)驗(yàn)證來(lái)顯示不同的主界面

root "welcome#index"

authenticated :user do
  root "users#index"
end
13.Keeping track with Annotate

使用annotate可以在model或者routes.rb中看到具體的schema或者路徑展開(kāi)。但是在rails中其實(shí)可以通過(guò)db/schema.rb和通過(guò)rails routes看到具體的模式普气。

gem 'annotate' #安裝
annotate  #執(zhí)行 
14.Sending emails with Mandrill

根據(jù)視頻提醒谜疤,這個(gè)應(yīng)用已經(jīng)開(kāi)始付費(fèi),暫時(shí)不看這個(gè)視頻现诀,相關(guān)的應(yīng)用也有Sendgrid, Postmark, MailJet, MailGun夷磕,遇到具體的項(xiàng)目再說(shuō),另外rails guides中用rails自帶的郵件發(fā)送也是可以的赶盔。

15.Understanding Semantic Versioning

主要是講了版本號(hào)的各種意義企锌,參考

x.y.z(for instance ruby 2.3.1)
主版本號(hào):當(dāng)你做了不兼容的 API 修改榆浓,
次版本號(hào):當(dāng)你做了向下兼容的功能性新增于未,
修訂號(hào):當(dāng)你做了向下兼容的問(wèn)題修正。
16.Button Loading Animations with jQuery UJS

該episode是介紹font awesome的一些圖標(biāo)陡鹃,這些圖標(biāo)已經(jīng)被內(nèi)建為jquery的樣式烘浦,在rails的程序中默認(rèn)已經(jīng)內(nèi)建。

//= require jquery
//= require jquery_ujs

使用方式如下萍鲸,當(dāng)表單提交不成功的時(shí)候會(huì)出現(xiàn)一種中間狀態(tài)闷叉,下面是rails默認(rèn)提交表單和simpleform表單的使用方式:

#默認(rèn)形式,使用scaffold的形式是f.submit脊阴,需要更改為f.
<%= f.button "Sign In", class: "btn btn-success", data: {disable_with: "<i class='fa fa-spinner fa-spin'></i> Signing in..."} %>

#simpleform形式
<%= f.button :button, "Save Post".html_safe, data: {disable_with: "<i class='fa fa-spinner fa-spin'></i> Saving..."} %>
17.jQuery UJS and AJAX

rails內(nèi)建了jquery,jquery支持ajax操作握侧,該episode通過(guò)實(shí)例介紹了通過(guò)ajax操作項(xiàng)目。項(xiàng)目說(shuō)明嘿期,post這個(gè)model有一個(gè)published_at字段品擎,通過(guò)publish鏈接,點(diǎn)擊無(wú)刷新頁(yè)面更新這個(gè)字段的值备徐,下面介紹相關(guān)的步驟:

#post具有published_at這個(gè)日期字段
#設(shè)置routes.rb,其中rails routes #=>publish_post_path
resources :posts do
  member do
    patch :publish
  end
end

#設(shè)置PostsController.rb文件
def publish
  @post.update(published_at: Time.zone.now)
end

#設(shè)置view文件萄传,出現(xiàn)remote: true的值時(shí),會(huì)自動(dòng)尋找publish.js.erb文件
<span id="published_at"><%= @post.published_at %></span>
<%= link_to "publish", publish_post_path(@post), method: :patch, remote: true %>

#添加js文件publish.js.erb文件
$("#published_at").text("<%= @post.published_at %>")
19.A Look Into Routing

介紹routes的內(nèi)容蜜猾,詳細(xì)的內(nèi)容guides中已經(jīng)做了介紹秀菱,這邊著重介紹了scope,member,collection振诬。

21.Rails 4.2 Introduction

介紹了rails4.2的兩個(gè)新功能,分別是active job和web console衍菱,其中web console是在網(wǎng)頁(yè)出現(xiàn)錯(cuò)誤的情況下網(wǎng)頁(yè)界面出現(xiàn)調(diào)試界面赶么,類似于BetterErrors這個(gè)gem。

23.Consuming an API Using HTTParty and Creating a Gem

這個(gè)視頻通過(guò)HTTParty這個(gè)gem來(lái)獲取相關(guān)api的內(nèi)容梦碗,其中在頁(yè)面端獲取api的信息禽绪,可以通過(guò)安裝一個(gè)chrome的插件JSONView,這個(gè)插件可以通過(guò)下面的方式更好的呈現(xiàn)json內(nèi)容:

http://localhost:3000/users/1.json

另外一個(gè)內(nèi)容是通過(guò)bundle命令來(lái)生成一個(gè)gem洪规,使用的系統(tǒng)是animatedgif.me

獲取上面網(wǎng)站的api可以通過(guò)如下方式:

#每次刷新印屁,內(nèi)容都是不同的
http://animatedgif.me/cat.json

下面介紹創(chuàng)建一個(gè)gem并且使用httparty這個(gè)gem

#使用bundle建立gem的命令行
bundle gem animated_demo

#animated_demo.gemspec中添加httparty以及修改spec.homepage
spec.homepage      = ""  #必須修改,不然會(huì)報(bào)錯(cuò)
spec.add_dependency 'httparty', '~> 0.14.0'

#修改內(nèi)容lib/animated_demo.rb
require "bundler/setup"  #必須添加斩例,不然irb會(huì)報(bào)錯(cuò)
require "animated_demo/version"
require "httparty"

module AnimatedDemo
  include HTTParty

  base_uri "animatedgif.me" #添加url

  def self.find(id)
    retrieve_url get("/gifs/#{id}.json")
  end

  def self.tagged(tag)
    retrieve_url get("/#{tag}.json")

  end

  def self.random(tag)
    tagged(tag)
  end

  private
    def self.retrieve_url(response)
      response.parsed_response["url"]
    end
end

#irb進(jìn)行操作和測(cè)試
require_relative 'lib/animated_demo'  #必須在animated_demo.rb添加require "bundler/setup"
#上面的值放回true
AnimatedDemo #返回AnimatedDemo
AnimatedDemo.find(10) #測(cè)試成功

#上傳到github中雄人,如果是在rails程序引用,需要要程序中使用下列語(yǔ)句
gem 'animated_demo', git: 'https://github.com/Jayzen/animated_demo.git'







25.Forum Series Part 1: Architecture

這個(gè)章節(jié)主要說(shuō)明項(xiàng)目的數(shù)據(jù)庫(kù)結(jié)構(gòu)以及需要安裝的gem文件:
source code url

#數(shù)據(jù)庫(kù)文件
User - Devise

email:string
password:string
has_many :forum_threads has_many :forum_posts

ForumThread

user_id:integer
subject:string
belongs_to :user has_many :forum_posts

ForumPost

forum_thread_id:integer
user_id:interger
body:text
belongs_to :forum_thread belongs_to :user

#gemfile文件
group :development do
  gem "annotate"
  gem "better_errors"
  gem "binding_of_caller"
end

gem "devise"

#安裝語(yǔ)句
rails g annotate:install
rails g devise:install
rails g devise:views
rails g devise User

rails g model ForumThread user_id:integer subject:string
rails g model ForumPost user_id:integer forum_thread_id:integer body:text
26.Forum Series Part 2: Routes

這個(gè)episode主要講述的是routes的用法念赶,這里用到了嵌套路由础钠,railsguides中已經(jīng)做了比較詳細(xì)的介紹,具體代碼如下所示:

resources :forum_threads do
  resources :forum_posts, module: :forum_threads
end

這里module的作用是應(yīng)用的目錄結(jié)構(gòu)中體現(xiàn)路由關(guān)系叉谜,需要在controller文件夾中放置form_threads文件夾旗吁,其中放置forum_posts_controller.rb文件,在views文件夾中放置forum_threads文件夾停局。

27.Forum Series Part 3: Nested Attributes and fields_for

內(nèi)容已經(jīng)在guides的view的第三節(jié)已經(jīng)有介紹很钓,而且考慮的已經(jīng)比較全面。需要注意的是rails5中的belongs_to的關(guān)聯(lián)董栽,必須關(guān)聯(lián)對(duì)象已經(jīng)存在的情況下才能保存码倦,為了避免這個(gè)問(wèn)題,需要進(jìn)行如下的設(shè)置:

belongs_to :post, optional: true
33.Sign In With Twitter using Omniauth and the Twitter gem

該視頻的主題如標(biāo)題內(nèi)容所示锭碳,使用的gem是omniauth-twitter這個(gè)gem袁稽,不需要添加其他的gem,思路主要是通過(guò)在本地的系統(tǒng)中建立一個(gè)user模型,分別與omniauth-twitter提供的字段對(duì)應(yīng)進(jìn)行存儲(chǔ)擒抛。
需要到twitter的官網(wǎng)進(jìn)行設(shè)置推汽,地址,其中callback url的地址需要為:

http://127.0.0.1:3000/auth/twitter/callback

記錄api key和api secret的值歧沪,并且改變相應(yīng)的permissions權(quán)限歹撒。上面的key和secret的記錄方式為:

#config/secrets.rb
development:
  twitter_api_key: DRZKK44rHuZt13Rp4dc2Babnr
  twitter_api_secret: 7Rw5U0QzeXNmR6heCrLpgLfu4Bvr5Dcfi7PJJFIA4fMMddTBOM

#config/initializer/omniauth.rb
Rails.application.config.middleware.use OmniAuth::Builder do
  provider :twitter, Rails.application.secrets.twitter_api_key, Rails.application.secrets.twitter_api_secret
end

#gemfile
gem 'omniauth-twitter'

下面的代碼為全部

#使用的twitter的鏈接地址為
#http://127.0.0.1:3000/auth/twitter

#設(shè)置相應(yīng)的routes.rb
resources :twitters
root 'twitters#index'
get '/auth/:provider/callback', to: "sessions#create"

#建立相應(yīng)的model
rails g model user provider uid name token secret profile_image

#user.rb
class User < ApplicationRecord
  def self.find_or_create_from_auth_hash(auth_hash)
    user = where(provider: auth_hash.provider, uid: auth_hash.uid).first_or_create
    user.update(
      name: auth_hash.info.name,
      profile_image: auth_hash.info.image,
      token: auth_hash.credentials.token,
      secret: auth_hash.credentials.secret
    )
    user
  end
end

#sessions_controller.rb
class SessionsController < ApplicationController
  def create
    #rails :test   #可以使用左邊這種方式進(jìn)行debug,輸入auth_hash查看全部的值
    @user = User.find_or_create_from_auth_hash(auth_hash)
    session[:user_id] = @user.id
    redirect_to root_path
  end

  protected
    def auth_hash
      request.env['omniauth.auth']
    end
end

#application_controller.rb
class ApplicationController < ActionController::Base
  protect_from_forgery with: :exception

  def current_user
    @current_user ||= User.find(session[:user_id]) if session[:user_id]
  end
  helper_method :current_user #helper方法可以在view中被使用槽畔,不然在controller中無(wú)效
end

#layouts/application.html.erb
<% if current_user %>
  <%= current_user.name %>
<% end %>

該episode中還講述了在項(xiàng)目中發(fā)布twitter的用法栈妆,但是基本上沒(méi)有用到,項(xiàng)目需求過(guò)程中再說(shuō)。

41.Soft Delete with Paranoia

gitbook中已經(jīng)有總結(jié)

53.How To Setup Vagrant For Rails Development

該視頻是用vagrant來(lái)提供在不同的本地環(huán)境中部署和開(kāi)發(fā)rails項(xiàng)目鳞尔,同時(shí)使用了chef的自動(dòng)化部署(這個(gè)是不熟悉的嬉橙,以后需要掌握)。本視頻文檔地址寥假。

61.Manage Assets With Rails Assets

使用rails assets來(lái)管理assets,rails默認(rèn)使用assets pipeline來(lái)管理assets,這個(gè)episode的初衷以及益處是什么需要再深入理解assets pipeline的時(shí)候再做分析市框。rails assets的地址和使用方式如下:

#以使用bootstrap為例

#gemfile文件
source 'https://rails-assets.org' do
  gem 'rails-assets-bootstrap'
end

#application.js文件
//= require bootstrap

#application.css文件
/* 
*= require_self
 *= require bootstrap
 *= require_tree
*/
84.What Is A State Machine?

解釋什么是STM(finite state machine),用wiki的解釋為:是表示有限個(gè)狀態(tài)以及在這些狀態(tài)之間的轉(zhuǎn)移和動(dòng)作等行為的數(shù)學(xué)模型。

85.The State_Machine Gem

介紹state_machine這個(gè)gem,看的不是很明白糕韧,不過(guò)應(yīng)該是在進(jìn)行交易的時(shí)候使用枫振,介紹交易過(guò)程中出現(xiàn)的各種狀態(tài),功能是為需求而生的萤彩,下次遇到具體項(xiàng)目的時(shí)候再回過(guò)頭來(lái)看STM粪滤。

88.Using Bootstrap 4 Rubygem with Rails

使用BT4
github中的bt4使用幫助

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市雀扶,隨后出現(xiàn)的幾起案子杖小,更是在濱河造成了極大的恐慌,老刑警劉巖愚墓,帶你破解...
    沈念sama閱讀 216,651評(píng)論 6 501
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件予权,死亡現(xiàn)場(chǎng)離奇詭異,居然都是意外死亡浪册,警方通過(guò)查閱死者的電腦和手機(jī)扫腺,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,468評(píng)論 3 392
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái)村象,“玉大人笆环,你說(shuō)我怎么就攤上這事∩飞觯” “怎么了咧织?”我有些...
    開(kāi)封第一講書(shū)人閱讀 162,931評(píng)論 0 353
  • 文/不壞的土叔 我叫張陵嗓袱,是天一觀的道長(zhǎng)籍救。 經(jīng)常有香客問(wèn)我,道長(zhǎng)渠抹,這世上最難降的妖魔是什么蝙昙? 我笑而不...
    開(kāi)封第一講書(shū)人閱讀 58,218評(píng)論 1 292
  • 正文 為了忘掉前任,我火速辦了婚禮梧却,結(jié)果婚禮上奇颠,老公的妹妹穿的比我還像新娘。我一直安慰自己放航,他們只是感情好烈拒,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,234評(píng)論 6 388
  • 文/花漫 我一把揭開(kāi)白布。 她就那樣靜靜地躺著,像睡著了一般荆几。 火紅的嫁衣襯著肌膚如雪吓妆。 梳的紋絲不亂的頭發(fā)上,一...
    開(kāi)封第一講書(shū)人閱讀 51,198評(píng)論 1 299
  • 那天吨铸,我揣著相機(jī)與錄音行拢,去河邊找鬼。 笑死诞吱,一個(gè)胖子當(dāng)著我的面吹牛舟奠,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播房维,決...
    沈念sama閱讀 40,084評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開(kāi)眼沼瘫,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼!你這毒婦竟也來(lái)了咙俩?” 一聲冷哼從身側(cè)響起晕鹊,我...
    開(kāi)封第一講書(shū)人閱讀 38,926評(píng)論 0 274
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤,失蹤者是張志新(化名)和其女友劉穎暴浦,沒(méi)想到半個(gè)月后溅话,有當(dāng)?shù)厝嗽跇?shù)林里發(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,341評(píng)論 1 311
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡歌焦,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,563評(píng)論 2 333
  • 正文 我和宋清朗相戀三年飞几,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片独撇。...
    茶點(diǎn)故事閱讀 39,731評(píng)論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡屑墨,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出纷铣,到底是詐尸還是另有隱情卵史,我是刑警寧澤,帶...
    沈念sama閱讀 35,430評(píng)論 5 343
  • 正文 年R本政府宣布搜立,位于F島的核電站以躯,受9級(jí)特大地震影響,放射性物質(zhì)發(fā)生泄漏啄踊。R本人自食惡果不足惜忧设,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,036評(píng)論 3 326
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望颠通。 院中可真熱鬧址晕,春花似錦、人聲如沸顿锰。這莊子的主人今日做“春日...
    開(kāi)封第一講書(shū)人閱讀 31,676評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)。三九已至刘陶,卻和暖如春狐血,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背易核。 一陣腳步聲響...
    開(kāi)封第一講書(shū)人閱讀 32,829評(píng)論 1 269
  • 我被黑心中介騙來(lái)泰國(guó)打工匈织, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人牡直。 一個(gè)月前我還...
    沈念sama閱讀 47,743評(píng)論 2 368
  • 正文 我出身青樓缀匕,卻偏偏與公主長(zhǎng)得像,于是被迫代替她去往敵國(guó)和親碰逸。 傳聞我的和親對(duì)象是個(gè)殘疾皇子乡小,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,629評(píng)論 2 354

推薦閱讀更多精彩內(nèi)容