How to return the correct Location to route with Resources in the singular?

Asked

Viewed 47 times

0

When using the controller name in the singular, the path is prefixed with _index at the end of the route name (ex. dispute_conference_index). Inside the controller in the method create the parameter location in render receives the registration URL.

Unfortunately the name passed to location is dispute_conference_url instead of dispute_conference_index.

See below the route configuration I’m using:

resources :disputes, shallow: true do
  scope module: :dispute do
    resources :conference, except: :index
  end
end

See the details of Routes:

                 Prefix Verb    URI Pattern                                Controller#Action
conferences              GET    /conferences(.:format)                     conferences#index {:format=>:json}
dispute_conference_index POST   /disputes/:dispute_id/conference(.:format) dispute/conference#create {:format=>:json}
conference               GET    /conference/:id(.:format)                  dispute/conference#show {:format=>:json}
                         PATCH  /conference/:id(.:format)                  dispute/conference#update {:format=>:json}
                         PUT    /conference/:id(.:format)                  dispute/conference#update {:format=>:json}
                         DELETE /conference/:id(.:format)                  dispute/conference#destroy {:format=>:json}
                         PUT    /conference/:id/start(.:format)            dispute/conference#start {:format=>:json}
                         PUT    /conference/:id/cancel(.:format)           dispute/conference#cancel {:format=>:json}
                         PUT    /conference/:id/finish(.:format)           dispute/conference#finish {:format=>:json}

What is the correct way to correct this question, passing the location correct?


Correcting the question: The reason for the error is because of the parameter shallow: true, it alters the path routes requiring manual intervention or through a function (url_path).

When trying to rotate the method create an error will be obtained as it will not be possible to find the location.

  • Adding statically is not a good idea... location: :dispute_conference_index

1 answer

0


After several attempts and errors the solution seemed. First it should be moved shallow: true for the call of resources and finally the attribute should be added shallow_prefix: :dispute. See below how it looks:

resources :disputes do
  scope module: :dispute, except: :index do
    resources :conference, shallow: true, shallow_prefix: :dispute
  end
end

Done that routes shall be assembled as desired:

Prefix Verb         URI Pattern Controller#Action
conferences              GET    /conferences(.:format)                     conferences#index {:format=>:json}
dispute_conference_index POST   /disputes/:dispute_id/conference(.:format) dispute/conference#create {:format=>:json}
dispute_conference       GET    /conference/:id(.:format)                  dispute/conference#show {:format=>:json}
                         PATCH  /conference/:id(.:format)                  dispute/conference#update {:format=>:json}
                         PUT    /conference/:id(.:format)                  dispute/conference#update {:format=>:json}
                         DELETE /conference/:id(.:format)                  dispute/conference#destroy {:format=>:json}
                         PUT    /conference/:id/start(.:format)            dispute/conference#start {:format=>:json}
                         PUT    /conference/:id/cancel(.:format)           dispute/conference#cancel {:format=>:json}
                         PUT    /conference/:id/finish(.:format)           dispute/conference#finish {:format=>:json}

Note that my controller is mounted on the module dispute this is also related to the problem.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.