如果在views / abouts /中我有“index.html.haml”和“history.html.haml” 如何访问abouts#history是一个基本的html页面。
从日志中我得到此错误,我猜它是将它作为节目处理,我该怎么办?:
Processing by AboutsController#show as HTML
Parameters: {"id"=>"history"}
About Load (0.3ms) SELECT `abouts`.* FROM `abouts` WHERE (`abouts`.`id` = 0) LIMIT 1
ActiveRecord::RecordNotFound (Couldn't find About with ID=history):
routes.rb
scope() do
resources :abouts, :path => 'about-us' do
match 'about-us/history' => "about-us#history"
end
end
abouts_controller.rb
def history
respond_to do |format|
format.html
end
end
答案 0 :(得分:2)
一些问题。首先,您应该匹配'history'
而不是'about-us/history'
(路由是嵌套的,因此会自动包含'about-us/'
部分)。其次,您需要使用:on => :collection
选项指定路径应与集合匹配,而不是集合的成员。最后,您应该将匹配路由到'abouts#history'
而不是'about-us#history'
(因为无论路由时使用哪个路径字符串,控制器都会命名为abouts
。
所以试试这个:
resources :abouts, :path => 'about-us' do
match 'history' => "abouts#history", :on => :collection
end
另请注意,match
将匹配所有 HTTP请求:POST
以及GET
。我建议使用get
而不是match
来将HTTP请求类型缩小到仅GET
个请求:
resources :abouts, :path => 'about-us' do
get 'history' => "abouts#history", :on => :collection
end
希望有所帮助。