Change default method mappings?

Currently these mappings are established somewhere, but I haven’t found it

:get => ‘index’, ‘show’

:post => ‘create’

:delete => ‘destroy’

:put => ‘edit’

In particular I want to add

:options => ‘options’

AJ ONeal

AJ ONeal wrote:

Currently these mappings are established somewhere, but I haven't found it :get => 'index', 'show' :post => 'create' :delete => 'destroy' :put => 'edit'

In particular I want to add :options => 'options'

No you don't. You probably want :get => 'options'.

Here and in your other post, you're confusing HTTP methods with Rails controller methods. I suggest rereading the routing guide.

AJ ONeal

Best,

> Currently these mappings are established somewhere, but I haven't found > it > :get => 'index', 'show' > :post => 'create' > :delete => 'destroy' > :put => 'edit'

> In particular I want to add > :options => 'options'

No you don't. You probably want :get => 'options'.

Ah, but, I do!

Here and in your other post, you're confusing HTTP methods with Rails controller methods.

Ah, but I'm not

Firefox and Safari / Chrome pre-flight CORS / XHR2 requests with OPTIONS before GET/POST/PUT/DELETE to see if the server allows CORS before attempting the request. Internet Explorer, I believe, just uses the HTTP vars from the original request.

XHR2 Headers such as X-Requested-With X-HTTP-Method-Override Content-Type Accept must be pre-approved with OPTIONS or the request will fail.

AJ ONeal

Firefox and Safari / Chrome pre-flight CORS / XHR2 requests with OPTIONS before GET/POST/PUT/DELETE to see if the server allows CORS before attempting the request. Internet Explorer, I believe, just uses the HTTP vars from the original request.

Make sense. Does :options => 'options' not work at all or are you just getting tired of typing it over and over again ? The former seems unlikely, but I haven't tried. If just the latter then with_options might be of use. You could also try overriding map_default_collection_actions on AC::Resources::Resource

Fred

Make sense. Does :options => 'options' not work at all or are you just getting tired of typing it over and over again ? The former seems unlikely, but I haven't tried. If just the latter then with_options might be of use. You could also try overriding map_default_collection_actions on AC::Resources::Resource

Fred

Thanks Fred,

Here's the monkey patch for Rails 2.3.3. I imagine it should be easy to modify for other versions:

module ActionController   module Resources     class Resource #:nodoc:       DEFAULT_ACTIONS = :index, :create, :new, :edit, :show, :update, :destroy, :options #Options Patch     end

    private       def map_member_actions(map, resource)         resource.member_methods.each do |method, actions|           actions.each do |action|             [method].flatten.each do |m|               action_path = resource.options[:path_names][action] if resource.options[:path_names].is_a?(Hash)               action_path ||= Base.resources_path_names[action] || action

              map_resource_routes(map, resource, action, "# {resource.member_path}#{resource.action_separator}#{action_path}", "# {action}_#{resource.shallow_name_prefix}#{resource.singular}", m, { :force_id => true })             end           end         end

        route_path = "#{resource.shallow_name_prefix}# {resource.singular}"         map_resource_routes(map, resource, :show, resource.member_path, route_path)         map_resource_routes(map, resource, :update, resource.member_path, route_path)         map_resource_routes(map, resource, :destroy, resource.member_path, route_path)         map_resource_routes(map, resource, :options, resource.path, route_path) #Options Patch       end

      def action_options_for(action, resource, method = nil, resource_options = {})         default_options = { :action => action.to_s }         require_id = !resource.kind_of?(SingletonResource)         force_id = resource_options[:force_id] && !resource.kind_of? (SingletonResource)

        case default_options[:action]           when "index", "new"; default_options.merge(add_conditions_for (resource.conditions, method || :get)).merge(resource.requirements)           when "create"; default_options.merge(add_conditions_for (resource.conditions, method || :post)).merge(resource.requirements)           when "show", "edit"; default_options.merge(add_conditions_for (resource.conditions, method || :get)).merge(resource.requirements (require_id))           when "update"; default_options.merge(add_conditions_for (resource.conditions, method || :put)).merge(resource.requirements (require_id))           when "destroy"; default_options.merge(add_conditions_for (resource.conditions, method || :delete)).merge(resource.requirements (require_id))           when "options"; default_options.merge(add_conditions_for (resource.conditions, method || :options)).merge (resource.requirements) #Options Patch           else default_options.merge(add_conditions_for (resource.conditions, method)).merge(resource.requirements(force_id))         end       end   end end

On ubuntu the original file is found at: /var/lib/gems/1.8/gems/actionpack-2.3.3/lib/action_controller/ resources.rb

And the patch should be placed in your Rails project folder in ./lib/resources.rb

Once installed you can specify options on a per-controller basis like so:

app/controllers/guest_sessions_controller.rb:   # Create a session for non-logged in users which may be merged on login or remain anonymous, one-time use.   def create     #TODO how to handle this in the case of internationalization     # I chose not to put this in the db layer because this is shared with the user class     guest = Guest.new({:display_name=>'Guest'})     guest.save     guest_session = GuestSession.new(guest)     # sanitize the outbound hash now to keep it dry when we add xml and html support in addition to json     hash = {:display_name => guest.display_name, :single_access_token => guest.single_access_token}     respond_to do |format|       # TODO use client ACCEPT headers rather than .:format. That's why we have HTTP standards.       format.json { render :json => hash, :callback => params [:callback] }     end   end

  # Log out   def destroy     #TODO if the guest didn't leave an e-mail, delete the guest (regarding as spam)     current_guest_session.destroy     message = {:message => "Logout successful!", :errors => }     respond_to do |format|       format.json { render :json => message, :callback => params [:callback] }     end   end

  # Show HTTP OPTIONS for XHR2 / CORS requests   def options     render :nothing => true, :status => 204     response.headers['Access-Control-Allow-Origin'] = '*'     response.headers['Access-Control-Allow-Methods'] = 'POST, GET, PUT, DELETE, OPTIONS'     response.headers['Access-Control-Allow-Credentials'] = 'true'     response.headers['Access-Control-Max-Age'] = '86400' # 24 hours     response.headers['Access-Control-Allow-Headers'] = 'X-Requested- With, X-HTTP-Method-Override, Content-Type, Accept'   end

Just to throw out some keyword for my fellow Googlers: You need this patch for Microsoft Discovery Protocol, WebDAV, DAV, XHR2, XDR (Cross-Domain Request) and CORS to work with Ruby on Rails for Firefox 3.5+, Chrome 4+, Safari 4+, and IE8+