0

如果在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):

路线.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
4

1 回答 1

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

希望有帮助。

于 2012-12-22T05:18:21.987 回答