在 Rails MVC 中对控制器和视图进行分组 [英] Grouping controllers and views in Rails MVC

查看:55
本文介绍了在 Rails MVC 中对控制器和视图进行分组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想做一些类似于这里的问题:MVC 中的分组视图、控制器、模型

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

但是在 Ruby on Rails 中.问题是,模型要分组在一个文件夹中,但控制器和视图要放在不同的文件夹中,比如 admin/和 store/文件夹.

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

这样,gallery_project_path(@project) 返回/gallery/projects/xxx(我希望它是/projects/xxx)

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

Rails 可以做到这一点吗?

Is this possible with Rails?

推荐答案

如果您只想要 /projects 路由,那么 map.namespace 不是您想要的选项.尝试只指定控制器的名称

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'

应该会生成这样的路由

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

您还应该查看 RailsGuide:Rails Routing from the Outdoor In.当我遇到路由问题时,我总是从那里开始.

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

这篇关于在 Rails MVC 中对控制器和视图进行分组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆