0

如果在树结构中有一些静态页面。第一级使用 static_pages_controller.rb 提供服务,在我的 routes.rb 中我有:

get '/static_pages/news'  , :to => 'static_pages#news'  , :as => 'news'
get '/static_pages/index' , :to => 'static_pages#index' , :as => 'index'
....

以上存在于

app\views\static_pages\news.html.erb
app\views\static_pages\index.html.erb
....

现在,我在 static_pages 根目录下粘贴了一些其他静态页面:

app\views\static_pages\ermis\news.html.erb
app\views\static_pages\ermis\index.html.erb
....

我在 routes.rb 中添加了这个:

get '/static_pages/ermis/news'  , :to => 'static_pages#news'    , :as => 'news'
get '/static_pages/ermis/index' , :to => 'static_pages#index'   , :as => 'index'

以上不起作用,因为操作已经存在(父文件夹)。所以我采取了将文件重命名为的痛苦步骤(但必须有更好的方法?!?)

app\views\static_pages\ermis\ermisnews.html.erb
app\views\static_pages\ermis\ermisindex.html.erb
....

我的 routes.rb 变成了

get '/static_pages/ermis/news' , :to => 'static_pages#ermisnews' , :as => 'ermisnews'
get '/static_pages/ermis/index', :to => 'static_pages#ermisindex', :as => 'ermisindex'
....

控制器为空

class StaticPagesController < ApplicationController
end

为什么不能提供页面?我错过了什么?

当我点击

<%= link_to("Ermis", ermisnews_path, class: 'pictureTitle') %>

我明白了

"The action 'ermisnews' could not be found for StaticPagesController"

这是我的路线.rb

Prefix Verb    URI Pattern                                Controller#Action
root           GET    /                                   static_pages#index
ermisindex     GET    /static_pages/ermis/index(.:format) static_pages#ermisindex
ermisnews      GET    /static_pages/ermis/news(.:format)  static_pages#ermisnews
news           GET    /static_pages/news(.:format)        static_pages#news
index          GET    /static_pages/index(.:format)       static_pages#index

注意:使用直接指向 static_pages 上的 .erb 文件的链接时,我没有收到错误消息

<%= link_to("News"     , news_path          , class: 'pictureTitle')

Question: 
1) How can I use the same controller to also serve static pages underneath /static_pages eg. /static_pages/ermis 
2) Am I obliged to actually rename the files to have them represent unique actions?
4

2 回答 2

0

在 route.rb 中,将您的路线更改为:

  resources :static_pages do

    resources :ermis do

      get 'ermisnews' , :on => :collection

    end

  end

  match '/ermisnews'  => 'static_pages#ermisnews', :as => :news

然后运行 ​​rake 路线。

于 2014-08-26T15:31:33.660 回答
0

最终我找到了解决问题的方法:

  1. 创建了以下命名空间:

    命名空间:sp 做资源:ixanos 资源:ermis 结束

  2. 创建了以下控制器

    类 Sp::IxanosController < ApplicationController end

    类 Sp::ErmisController < ApplicationController end

  3. 将控制器放在 app/controllers/sp/ 下

  4. 创建目录 app/views/sp/ixanos 和 app/views/sp/ermis 并将我的文件复制到其中。

(*) 这样,我可以在给定的根(ermis 和 ixanos)下拥有任意数量的静态页面。我还没有测试过我将拥有像 sp/ermis/dir1/dir2 这样的子目录的情况......

于 2014-08-27T05:43:11.417 回答