rails&ruby**章节:3-4**
rails new sample_app --skip-test-unit 让rails不生成默认使用的 Test::Unit
生成controller 驼峰式 蛇底式
rails generate controller StaticPages (复数) home help (默认生成的方法) --no-test-framework
rails generate controller static_pages ... ...
撤销操作
rails destory controller StaticPages home help
rails destory model StaticPage
迁移命令
rake db:migrate
rake db:rollback
rake db:migrate VERSION=0 回到初始
rails中 mvc 对应的约定目录和对应关系
测试用例 - RSpec
rails generate integration_test static_pages
`` /spec/requests/static_pages.rb
require 'spec_helper'
describe "Static pages" do
describe "Home page" do
it "should have the content 'sample App'" do
visit '/static_pages/home'
expect(page).to have_content('Sample App')
end
end
end``
在 spec/spec_helper.rb 中添加一行新的代码
config.include Capybara::DSL
执行: bundle exec rspec ./spec/requests/static_pages.rb
...暂时放弃一下 太多坑了 ...
嵌入式ruby ~ Embedded ruby
<% provide( :title, 'Home') %>
<% yield(:title) %>
rails 中 provide 函数将 Home 赋值给title (:title 标记类 Symbol)
yield 中将 title 传到yield 中
application.html.erb -- 公共模板 erb 嵌入式ruby
掠过:
guard spork bundler binstubs rvm , sublime 中进行测试环境
Ruby -- rails背后的女人
定义一个helper 方法 :app/helpers/application_helper.rb
``def full_title page_title
base_title= "ruby on rails title base"
if page_title
base_title
else
"#{base_title}| #{page_title}"
end
end``
stylesheet_link_tag 'application', meida: 'all' , 'data-turbolinks-track': reload
javascript_include_tag 'application', 'data-turbolinks': 'reload'
rails console
"#{username}"
'' / ""
puts / gets
.length
.empty?
|| && !=
.to_s.to_i .to_a
nil
nil.to_s / nil.empty? / nil.to_s.empty? / nil.nill? / "".nil?
if unless while until case ...
def
(1..5) range类 数组类[] 哈希类{} 数组类和哈希类之间的区别?取出数组的某个值?
a[0] a[1] a[-1] a.first a.second a.last
a.sort a.reverse a.shuffle
a.push(7) / a<<7 / a<<7<<8
a.join "abcd"
a.join(',') "a,b,c,d"
(0..8).to_a .to_s
a= %w[a b c d e] => (数组) [a,b,c,d,e]
a[0..2] =>[a,b,c]
ruby 中的块
(1..5).each { |x| puts 2*x }
(1..5).each do |x| puts x end
3.times {puts 'a'}
(1..5).map {|i| i**3 }
%w[a b c].map{ |x| x.upcase}
=>['A','B','C']
%w[A B C ].map {|x| x.downcase}
=>['a','b','c']
("a".."z").to_a.shuffle[0..7].join
Hash 和 Symbol
"name".split('')
=>["n" , "a", "m", "e"]
user = {:name=>"mike",📧"350866797@qq.com"}
flash = { success: "It worked!", error: "It failed." }
flash.each do |key , value|
puts key,"=>",value
puts "#{key.inspect}, #{value.inspect}"
end
puts (1..5).to_a
puts (1..5).to_a.inspect
puts "It worked!", "It worked!".inspect
=>
It worked!
"It worked!"
p "name" 等价 puts "name"
stylesheet_link_tag("application", media: "all",
"data-turbolinks-track" => true)
stylesheet_link_tag "application", media: "all",
"data-turbolinks-track" => true
stylesheet_link_tag "application", { media: "all",
"data-turbolinks-track" => true }
stylesheet_link_tag "application", media: "all",
"data-turbolinks-track" => true
构造器
s = String.new("foobar")
s.class
a = Array.new([1, 3, 2])
h = Hash.new
h = Hash.new(0)
类的继承
.class / .superclass
Ruby 允许向内内置类中添加方法
class String
def palindrome?
self == self.reverse
end
end
=> nil
"deified".palindrome?
=> true
example_user.rb
class User
attr_accessor :name, :email
def initialize(attributes = {})
@name
= attributes[:name]
@email = attributes[:email]
end
def formatted_email
"#{@name} <#{@email}>"
end
end
require './example_user'
=> ["User"]
example = User.new
=> #<User:0x224ceec @email=nil, @name=nil>
example.name
nil
=> nil
example.name = "Example User"
nil
=> "Example User"
example.email = "user@example.com"
nil
=> "user@example.com"
example.formatted_email
=> "Example User user@example.com"

浙公网安备 33010602011771号