简体   繁体   English

Rails3:适当使用路由和资源

[英]Rails3: Appropriate use of routing and resources

I've recently joined the world of Rails app development (Rails3) and I may be abusing resourceful routing. 我最近加入了Rails应用程序开发(Rails3)的世界,我可能正在滥用资源路由。

The default resourceful routing makes some really convenient helper methods for the URLs which I use constantly. 默认的资源路由为我经常使用的URL提供了一些非常方便的帮助方法。 My problem is that I have controllers that I specified the routing as resourceful simply to take advantage of those helper methods. 我的问题是我有控制器,我将路由指定为资源丰富,只是为了利用这些辅助方法。 I have some basic site navigation that has no business with resources. 我有一些基本的网站导航,没有资源业务。

resource :home do
  member do
    get 'main'
    get 'about'
    get 'login'
    get 'help'
  end
end

Is there a better way to do what I've been doing? 有没有更好的方法去做我一直在做的事情? Anything that doesn't require that I manually add routing entries each time I have a new controller action? 什么不需要我每次有新的控制器操作时手动添加路由条目?

Just to clarify, I want to specify routing for a controller without having to explicitly add any new actions but I also want it to auto-generate helper methods. 为了澄清,我想为控制器指定路由,而不必显式添加任何新操作,但我也希望它自动生成辅助方法。 So far, I have to explicitly add routes for each action I want that for. 到目前为止,我必须为我想要的每个动作明确添加路由。 I can get something similar by doing this (in a non-resourceful way), 通过这样做我可以得到类似的东西(以非资源的方式),

match 'home/about' => 'home#about'

But I don't want to have to write that very every route that doesn't fall into the convention. 但我不想写出那些不属于惯例的路线。

Here's another simpler one. 这是另一个更简单的问题。 Just add a generic route to the bottom of your routes.rb 只需在routes.rb的底部添加一个通用路由即可

match ":controller/:action"

and it will map directly to the specified action of the specified controller. 它将直接映射到指定控制器的指定操作。 You can be a bit more specific if you like. 如果你愿意,你可以更具体一点。 For example, using get instead of match to restrict to HTTP GET requests, specifying the applicaple controllers etc. 例如,使用get而不是match来限制HTTP GET请求,指定applicaple控制器等。

get ":controller/:action", :constraints => { :controller => /home|help/ }

You can look into your controller for public instance methods and generate routes automatically. 您可以查看控制器的公共实例方法并自动生成路由。

# routes.rb
HomeController.public_instance_methods(false).select{|m| !(m.to_s =~ /^_/)}.each do |m|
  match "home/#{m}", :action => m, :controller => HomeController, :as => "home_#{m}"
end

This will take the explicit(non-inherited) public instance methods from your controller, and select the ones that don't begin with an underscore(because underscored ones are generated methods for filters, the rest are actual actions). 这将采用控制器中的显式(非继承)公共实例方法,并选择以下划线开头的方法(因为下划线的是生成过滤器的方法,其余的是实际操作)。 Then it will generate a named route for each. 然后它将为每个生成一个命名路由。

Keep in mind that routes.rb is processed only at server startup so you will have to restart the server after you add new actions. 请记住, routes.rb仅在服务器启动时处理,因此您必须在添加新操作后重新启动服务器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM