这是我目前正在工作的routes.rb文件:
Rails.application.routes.draw do
get 'static_pages/help'
get 'static_pages/test', as: "can_do_this"
get 'static_pages/home', to: "static_pages#home", as: "home"
root 'application#hello'
end
但是,如果我添加以下行:
Rails.application.routes.draw do
resources :static_pages #added this line
get 'static_pages/help'
get 'static_pages/test', as: "can_do_this"
get 'static_pages/home', to: "static_pages#home", as: "home"
root 'application#hello'
end
然后我的代码中断,我的页面上的任何内容都没有显示。有人可以向我解释这条线的作用以及如何使用它吗?
答案 0 :(得分:0)
资源:static_pages将为您创建这7条路线
GET /static_pages static_pages#index
GET /static_pages/new static_pages#new
POST /static_pages static_pages#create
GET /static_pages/:id static_pages#show
GET /static_pages/:id/edit static_pages#edit
PATCH/PUT /static_pages/:id static_pages#update
DELETE /static_pages/:id static_pages#destroy
虽然您需要让Controller代表static_pages以及所需的视图。
希望这会有所帮助..
如果您还有任何疑问,请与我们联系。
答案 1 :(得分:0)
在routes.rb
resources :static_pages
将生成以下宁静路线
static_pages GET /static_pages(.:format) static_pages#index
POST /static_pages(.:format) static_pages#create
new_static_page GET /static_pages/new(.:format) static_pages#new
edit_static_page GET /static_pages/:id/edit(.:format) static_pages#edit
static_page GET /static_pages/:id(.:format) static_pages#show
PATCH /static_pages/:id(.:format) static_pages#update
PUT /static_pages/:id(.:format) static_pages#update
DELETE /static_pages/:id(.:format) static_pages#destroy
答案 2 :(得分:0)
如其他答案中所述,resources :static_pages
会创建多条路线。其中有一个:
static_page GET /static_pages/:id(.:format) static_pages#show
因此,当您请求http://localhost:3000/static_pages/help时,此路由与此网址匹配。它使用参数StaticPagesController#show
调用{'id' => "help"}
操作。您的自定义help
操作未被考虑,因为已找到匹配的路由。
这里有几种可能的方式:
show
操作(按ID)提供静态页面并删除自定义路线。resources :static_pages
放在自定义路线之后,以便自定义路线首先匹配。