Skip to content
Browse files

Allow alternative values for the 'new' and 'edit' actions in resource…

…ful routes.

map.resource :schools, :as => 'escuelas', :path_names => { :new => 'nueva' }

Closes #11181.  [ivanvr]
  • Loading branch information...
1 parent 4364c36 commit 063c393bf0a2eb762770c97f925b7c2867361ad4 @NZKoz NZKoz committed Apr 12, 2008
View
4 actionpack/lib/action_controller/base.rb
@@ -337,6 +337,10 @@ class Base
@@resource_action_separator = "/"
cattr_accessor :resource_action_separator
+ # Allow to override path names for default resources' actions
+ @@resources_path_names = { :new => 'new', :edit => 'edit' }
+ cattr_accessor :resources_path_names
+
# Sets the token parameter name for RequestForgery. Calling #protect_from_forgery sets it to :authenticity_token by default
cattr_accessor :request_forgery_protection_token
View
21 actionpack/lib/action_controller/resources.rb
@@ -80,7 +80,9 @@ def path
end
def new_path
- @new_path ||= "#{path}/new"
+ new_action = self.options[:path_names][:new] if self.options[:path_names]
+ new_action ||= Base.resources_path_names[:new]
+ @new_path ||= "#{path}/#{new_action}"
end
def member_path
@@ -266,6 +268,13 @@ def initialize(entity, options)
# notes.resources :attachments
# end
#
+ # * <tt>:path_names</tt> - specify different names for the 'new' and 'edit' actions. For example:
+ # # new_products_path == '/productos/nuevo'
+ # map.resources :products, :as => 'productos', :path_names => { :new => 'nuevo', :edit => 'editar' }
+ #
+ # You can also set default action names from an environment, like this:
+ # config.action_controller.resources_path_names = { :new => 'nuevo', :edit => 'editar' }
+ #
# * <tt>:path_prefix</tt> - set a prefix to the routes with required route variables.
#
# Weblog comments usually belong to a post, so you might use resources like:
@@ -515,8 +524,14 @@ def map_member_actions(map, resource)
resource.member_methods.each do |method, actions|
actions.each do |action|
action_options = action_options_for(action, resource, method)
- map.named_route("#{action}_#{resource.name_prefix}#{resource.singular}", "#{resource.member_path}#{resource.action_separator}#{action}", action_options)
- map.named_route("formatted_#{action}_#{resource.name_prefix}#{resource.singular}", "#{resource.member_path}#{resource.action_separator}#{action}.:format",action_options)
+ action_path = action
+ if resource.options[:path_names]
+ action_path = resource.options[:path_names][action]
+ action_path ||= Base.resources_path_names[action]
@libc
libc added a note Apr 18, 2008

This introduce a bug: http://rails.lighthouseapp.com/projects/8994/tickets/19-resource-routes-with-path_name-and-member

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
+ end
+
+ map.named_route("#{action}_#{resource.name_prefix}#{resource.singular}", "#{resource.member_path}#{resource.action_separator}#{action_path}", action_options)
+ map.named_route("formatted_#{action}_#{resource.name_prefix}#{resource.singular}", "#{resource.member_path}#{resource.action_separator}#{action_path}.:format",action_options)
end
end
View
6 actionpack/test/controller/resources_test.rb
@@ -76,6 +76,12 @@ def test_default_restful_routes
end
end
+ def test_override_paths_for_default_restful_actions
+ resource = ActionController::Resources::Resource.new(:messages,
+ :path_names => {:new => 'nuevo', :edit => 'editar'})
+ assert_equal resource.new_path, "#{resource.path}/nuevo"
+ end
+
def test_multiple_default_restful_routes
with_restful_routing :messages, :comments do
assert_simply_restful_for :messages

3 comments on commit 063c393

@paxx2
paxx2 commented on 063c393 Apr 12, 2008

I just realized this patch can be used to also redefine member actions, but not collection actions. I’ll post an updated patch later. (I’m ivanvr)

@paxx2
paxx2 commented on 063c393 Apr 12, 2008

And I realize now that this is already included in Rails edge! Thanks!

I have to get used to this new site.

@mapmarkus

A very spected change for the non-english people.

Good work. Michael

Please sign in to comment.
Something went wrong with that request. Please try again.