samvera-deprecated / orcid

[DEPRECATED] A Rails engine for orcid.org integration.
https://github.com/projejcthydra-labs/orcid
Other
14 stars 7 forks source link

Fixing singular Rails resource behavior #23

Closed jeremyf closed 10 years ago

jeremyf commented 10 years ago

Since we have a singular resource for ProfileRequest, the Controller#respond_with was treating the profile_request parameter as the :format option (i.e. format = 'xml').

The solution was to tease apart the automagic behavior of the #respond_with method into its two response options for transformative actions - either render the page with errors or redirect due to success.

I believe this is related to https://github.com/rails/rails/issues/1769, which is called out in http://guides.rubyonrails.org/routing.html#singular-resources

Below is a more detailed breakdown of what I am refering to:

The ProfileRequest is a singular resource.

Orcid::Engine.routes.draw do
  scope module: 'orcid' do
    resource :profile_request, only: [:show, :new, :create]
    resources :profile_connections, only: [:new, :create, :index]
  end
end

As a singluar resource the following routes are available.

$ rake app:routes
Routes for Orcid::Engine:
   profile_request  POST /profile_request(.:format)     orcid/profile_requests#create
new_profile_request GET  /profile_request/new(.:format) orcid/profile_requests#new
                    GET  /profile_request(.:format)     orcid/profile_requests#show

If we had a plural resource for ProfileRequest

Orcid::Engine.routes.draw do
  scope module: 'orcid' do
    resources :profile_request, only: [:show, :new, :create]
    resources :profile_connections, only: [:new, :create, :index]
  end
end

Then the routes would be:

$ rake app:routes
profile_request_index POST /profile_request(.:format)     orcid/profile_request#create
  new_profile_request GET  /profile_request/new(.:format) orcid/profile_request#new
      profile_request GET  /profile_request/:id(.:format) orcid/profile_request#show
jeremyf commented 10 years ago

For reference http://ndlib.github.io/practices/rails-respond-with-and-singular-resources/