使用Rails 3路由添加自定义:new路由 [英] Adding custom :new routes using Rails 3 routing

查看:99
本文介绍了使用Rails 3路由添加自定义:new路由的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Rails 2中,我们可以向资源丰富的路由添加自定义 new 操作,例如:

In Rails 2 we can add custom new actions to resourceful routes, like:

map.resources :users, :new => {:apply => :get}

我们如何在Rails 3中实现同一目标?

How do we achieve the same thing in Rails 3?

resources :users do

  get :apply, :on => :new    # does not work

  new do
    get :apply               # also does not work
  end

end

有什么想法吗?

推荐答案

您可以将:path_names 用作解释在边缘路由指南中:

You can use :path_names as explained in the edge routing guide:

resources :users, :path_names => { :new => "apply" }

那只会将路径更改为 apply ,它仍将路由到 new 操作。我认为更改不再受到明确支持(这可能是一件好事)。

That will only change the path to apply, it will still be routed to the new action. I don't think changing that is explicitly supported anymore (which is probably a good thing).

如果您想保留您的应用操作,您可能应该这样做:

If you want to keep your apply action, you should probably do:

resources :users, :except => :new do
  collection do
    get :apply
  end
end

但是让您想知道将 apply 操作重命名为 new 是否更好

But it leaves you wondering whether it isn't better to just rename the apply action to new.

这篇关于使用Rails 3路由添加自定义:new路由的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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