Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to route your sub-folder in views Ruby on Rails?

Can anyone please shed some light on how to route your sub-folder's .html.erb files?? which is placed like this:

view/pages/en/index.html.erb

and to route this i am doing following things on route.rb

match ':lang/index', :to => 'pages/en#index'

and for a link code, I have this on the header

<%= link_to "Home", index_path %>

The error i am getting is

 Routing Error
 uninitialized constant Pages

routes:

enter image description here

like image 498
RajG Avatar asked Nov 05 '12 14:11

RajG


3 Answers

Namespaces will organize your code and views in subfolders: http://guides.rubyonrails.org/routing.html#controller-namespaces-and-routing

If just need only the views/pages folder organized that way, you could do in PagesController something like:

render "#{I18n.locale}/#{action_name}"

A question: why would you like view/pages/en/index.html.erb instead of view/pages/index.en.html.erb? That would work out of the box.

like image 167
TuteC Avatar answered Nov 16 '22 23:11

TuteC


UPDATE This is how it works for route.rb:-

match ':lang/index', :to => 'pages#index'

Render it on your controller:-

def index
  render "pages/en/index"
end

def about
  render "pages/#{params[:lang]}/about"
end
like image 39
RajG Avatar answered Nov 17 '22 01:11

RajG


AFAIK, There is no way to route to a view. You can route an URL to a controller's action. That action is responsible for rendering the views.

you can use namespaced routing to put the resources in the sub folder.

...

What i wanted to write already written by @TuteC. Just follow that link and yes you can get language specific thing out of box as he explained.

like image 23
HungryCoder Avatar answered Nov 16 '22 23:11

HungryCoder