$ rails destroy controller FooBars baz quux
如果你已經建立了複雜的controller的框架,router中已經設好了怎麼辦,沒關係,上面兩個語句完全是相反的,用吧,完全取消哦,親!
rails generate model Foo bar:string baz:integer
rails destroy model Foo
rake db:migrate rake db:rollback rake db:migrate VERSION=0 這個用來遷徙數據庫到你想要的版本,
rails generate controller StaticPages home help
get "static_pages/home"
maps requests for the URI /static_pages/home to the home action in the StaticPages controller.
The hypertext transfer protocol (HTTP) defines four basic operations, corresponding to the four verbs get, post, put, and delete.GET is the most common HTTP operation, used for reading data on the web; it just means “get a page”, and every time you
visit a site like google.com or wikipedia.org your browser is submitting a GET request. POST is the next most common operation; it is the request sent by your browser when you submit a form. In Rails applications, POST requests are typically used for creating
things. for example, the POST request sent when you submit a registration form creates a new user on the remote site. //get 當你訪問頁面的時候用到,Post當你創造了一些東西,比如說註冊之類的時候用到。The other two verbs, PUT and DELETE, are designed for updating and destroying things on the remote
server.
無需瀏覽器的代碼測試:
rails generate integration_test static_pages 生成測試代碼 在spec/request下面static_pages_spec.rb
最後通過bundle exec rspec spec/requests/static_pages_spec.rb 來測試結果
<% provide(:title, 'Home') %>indicates using <% ... %> that Rails should call the provide function and associate the string ’Home’ with the label :
title.14 Then, in the title, we use the closely related notation <%= ... %> to insert the title into the template using Ruby’s yield function:
<title>Ruby on Rails Tutorial Sample App | <%= yield(:title) %></title>
如何在erb文件裏嵌入ruby
(The distinction between the two types of embedded Ruby is that <% ... %> executes the code inside, while <%= ... %> executes it and inserts the result into the template.) 沒有等於號的只是執行一下代碼,有等於號的是執行完了還要貼到原來的位置。
<%= csrf_meta_tags %> 這個是爲了 prevents cross-site request forgery (CSRF), a type of malicious web attack.
application.html.erb
<!DOCTYPE html>
<html>
<head>
<title>Ruby on Rails Tutorial Sample App | <%= yield(:title) %></title>
<%= stylesheet_link_tag "application", :media => "all" %>
<%= javascript_include_tag "application" %>
<%= csrf_meta_tags %>
</head>
<body>
<%= yield %>
</body>
</html>
home.html.erb
<% provide(:title, 'Foo') %>
<!DOCTYPE html>
<html>
<body>
Contents
</body>
</html>
let(:base_title) { "Ruby on Rails Tutorial Sample App" } page.should have_selector('title', :text => "#{base_title} | Home")