Grouping controllers and views in Rails MVC

2019-09-06 13:03发布

问题:

I want to do something similar to the question here: Grouping Views, Controllers, Models in MVC

But in Ruby on Rails. And the catch is, Models are to be grouped in a folder, but Controllers and Views are to be put in different folders, say admin/ and store/ folders.

Tried to separate these in a project, but problem is:

  map.connect 'projects/:id', :controller => 'gallery/projects', :action => 'show'
  map.namespace(:gallery,  :active_scaffold => true) do |gallery|
    gallery.resources :projects, :comments
  end

With that, gallery_project_path(@project) returns /gallery/projects/xxx (I want it to be /projects/xxx)

Is this possible with Rails?

回答1:

If you just want /projects routes then map.namespace isn't the option you want. Try just specifiy the name of the controller

map.resources :projects, :controller => 'gallery/projects'

That should generate routes like these

GET /projects/:id(.:format) {:controller=>"gallery/projects", :action=>"show"}
PUT /projects/:id(.:format) {:controller=>"gallery/projects", :action=>"update"}

You should also check out the RailsGuide: Rails Routing from the Outside In. I always start there when I have routing issues.