Including attributes in custom Rails routes -
i hope title not misleading, don't know better title problem i'm working on:
i have doctor belongs location , specialty. i'd route show action of doc controller this:
/dentist/berlin/7
i defined routes this:
get ':specialty/:location/:id', to: 'docs#show'
and in views create following url link show action of doc controller:
<%= link_to doc.name, "#{doc.specialty.name}/#{doc.location.name}/#{doc.id}" %>
is solution problem? if not, there cleaner way construct urls possibly using resources? heck name problem?
thank your in advance.
for references, should have @ this page (especially the end of section 2.6)
if single route, it's okay did. if want have more 1 route (like /dentist/berlin/7
, /dentist/berlin/7/make_appointment
, etc.) might want structure bit more routes take advantage of rails resources
.
for example, instead of
get ':specialty/:location/:id', to: 'doctors#show' ':specialty/:location/:id/appointment', to: 'doctors#new_appointment' post ':specialty/:location/:id/appointment', to: 'doctors#post_appointment'
you have (the code equivalent, see explanation below)
resources :doctors, path: '/:specialty/:location', only: [:show] member 'new_appointment' post 'create_appointment' end end
explanation
resources
generate restful routes (index, show, edit, new, create, destroy) specified controller (doctors_controller
assume)- the 'only' means don't want add restful routes, ones specified
then want add
member
actions, ie. actions can executed on particular item of collection. can chose different syntaxesresources :doctors member # here have prefix /:id action applies particular item end end # or resources :doctors 'new_appointement', on: :member end
by default, controller action same path name give, can override it
member 'appointment', action: 'new_appointment' post 'appointment', action: 'post_appointment' end
rails has wonderful helpers when comes routing !
Comments
Post a Comment