ruby on rails - Is there a way to set the controller action for resources defined with only: or except:? -
i have nested nested resources:
resources :assessments member 'result' end resources :respondents, only: [:new, :create] collection post "invite", to: :invite_all "invite", to: :new_invite end end end
for line resources :respondents, only: [:new, :create]
possible set action new , crate actions? can use to:
set action single resource. i'd avoid writing match statements if can , keep things resourceful.
what motivates me ask i'd able specify action nested resource rather have route child resource's action. example:
if define
resources :assessments resources :respondents end
the path /assessments/:id/respondents/new
route respondents#new
. problem forces me add logic new action determine if route contains assessment id or not , render correct view. i'd able send nested resource different action. there "rails way" this?
why not not nest resources this:
resources :assessments member 'result' end end resources :respondents, only: [:new, :create] collection post "invite", to: :invite_all "invite", to: :new_invite end end
or keep nesting other actions (e.g. index) define new , create actions separately.
resources :assessments member 'result' end resources :respondents end resources :respondents, only: [:new], path_names: { new: 'make' } collection post :generate post "invite", to: :invite_all "invite", to: :new_invite end end
this create these routes:
result_assessment /assessments/:id/result(.:format) assessments#result assessment_respondents /assessments/:assessment_id/respondents(.:format) respondents#index post /assessments/:assessment_id/respondents(.:format) respondents#create new_assessment_respondent /assessments/:assessment_id/respondents/new(.:format) respondents#new edit_assessment_respondent /assessments/:assessment_id/respondents/:id/edit(.:format) respondents#edit assessment_respondent /assessments/:assessment_id/respondents/:id(.:format) respondents#show patch /assessments/:assessment_id/respondents/:id(.:format) respondents#update put /assessments/:assessment_id/respondents/:id(.:format) respondents#update delete /assessments/:assessment_id/respondents/:id(.:format) respondents#destroy assessments /assessments(.:format) assessments#index post /assessments(.:format) assessments#create new_assessment /assessments/new(.:format) assessments#new edit_assessment /assessments/:id/edit(.:format) assessments#edit assessment /assessments/:id(.:format) assessments#show patch /assessments/:id(.:format) assessments#update put /assessments/:id(.:format) assessments#update delete /assessments/:id(.:format) assessments#destroy generate_respondents post /respondents/generate(.:format) respondents#generate invite_respondents post /respondents/invite(.:format) respondents#invite_all /respondents/invite(.:format) respondents#new_invite new_respondent /respondents/make(.:format) respondents#new
this way new , create outside of of nested assessments.
i created these routes real quick locally , generated when running rake routes
.
Comments
Post a Comment