Rails用来自同一控制器的基本视图模拟嵌套资源



如果在views/paths/中我有"index.html. html "。"history.html.haml"one_answers"history.html.haml".
我怎么能访问大约#历史,这是一个基本的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

有几个问题。首先,您应该匹配'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请求:POSTGET。我建议使用get而不是match,将HTTP请求类型缩小到GET请求:
resources :abouts, :path => 'about-us' do
  get 'history' => "abouts#history", :on => :collection
end

希望对你有帮助。

最新更新