ror突进-p4

CRUD

C - 建立(Create)新资料并存进资料库。
R - 读取(Read)或显示资料库里的资料。
U - 更新(Update)既有的资料。
D - 删除(Destroy)资料。

这分别对应了数据库的四种主要的操作,貌似分别对应了web层RESTFUL的put、get、post、del

设定主页

修改config/routes.rb,添加一行root来设置主页的路由

Rails.application.routes.draw do
  root 'topics#index'
  resources :topics
  # For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html
end

查看路由

可以使用 rails routes 或者 http://localhost:3000/rails/info/routes 来查看当前的路由

localhost:testruby02 alan$ rails routes
    Prefix Verb   URI Pattern                Controller#Action
      root GET    /                          topics#index
    topics GET    /topics(.:format)          topics#index
           POST   /topics(.:format)          topics#create
 new_topic GET    /topics/new(.:format)      topics#new
edit_topic GET    /topics/:id/edit(.:format) topics#edit
     topic GET    /topics/:id(.:format)      topics#show
           PATCH  /topics/:id(.:format)      topics#update
           PUT    /topics/:id(.:format)      topics#update
           DELETE /topics/:id(.:format)      topics#destroy

网站交互

可以使用 rails console 进入交互式命令行来进行控制,并以此查看路由对应的变量

localhost:testruby02 alan$ rails console
Running via Spring preloader in process 54277
Loading development environment (Rails 5.0.0.1)
2.3.1 :001 > app.root_path
 => "/"
2.3.1 :002 > app.topics_path
 => "/topics"
2.3.1 :003 >