Search code examples
ruby-on-rails-4static-pages

serve static views from subdirs using a single controller in rails 4


If have some static pages in a tree structure. The ones at the first level are served using static_pages_controller.rb and in my routes.rb I have :

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

The above exist in

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

Now, I pasted some other static pages underneath the static_pages root:

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

I added in routes.rb this:

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

The above doesnt work because the actions already exist (parent folders). So I went the painful step of renaming the files to (there must be a better way though?!?)

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

and my routes.rb became

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

the controller is empty

class StaticPagesController < ApplicationController
end

Why cant the pages be served ? what am I missing?

When I click on

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

I get

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

Here my routes.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

NOTE: I do not get an error when using link directly pointing to .erb files on static_pages

<%= 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?

Solution

  • Eventually I found a solution to my issue:

    1. created the following namespace:

      namespace :sp do resources: ixanos resources: ermis end

    2. created the following controllers

      class Sp::IxanosController < ApplicationController end

      class Sp::ErmisController < ApplicationController end

    3. Placed the controllers under app/controllers/sp/

    4. Created the directories app/views/sp/ixanos and app/views/sp/ermis and copied my files in them.

    (*) This way I can have as many static pages as I want underneath the given roots (ermis and ixanos). I haven't tested the situation whereby I will have sub-directories like sp/ermis/dir1/dir2 ...