开发者

Adding custom :new routes using Rails 3 routing

开发者 https://www.devze.com 2023-01-03 07:13 出处:网络
In Rails 2 we can add custom new actions to resourc开发者_开发知识库eful routes, like: map.resources :users, :new => {:apply => :get}

In Rails 2 we can add custom new actions to resourc开发者_开发知识库eful routes, like:

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

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

Any ideas?


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

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

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

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


Try this:

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

Note that if you want to re-map the new route to apply for all your routes then you can use a scope:

scope :path_names => { :new => 'apply' } do
  # The rest of your routes go here...
end
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号