问题
Here comes a rails nub organizational question:
What exists:
A "Railsy" link would look something like this (in slim)
span
a href= signout_path
signout_path
correspond to AuthController#signout
.
So, in my applications there's several types of users - Firefighters, Chiefs and Captains.
Each one has very similar routes:
/dashboard
/dashboard/:date
( this is in case they want to see a previous date;
same dashboard, different data in the controller )
/account
/account/update
/entry/view
/entry/edit
Entries may or may not belong to a user. Because entries should be visible to any user (though not relevant actions)
Take a look at my disgusting mess of routes:
scope :firefighters do
get '/dashboard' => 'firefighters#dashboard'
get '/dashboard/:date' => 'firefighters#dashboard'
get '/account' => 'account#account'
post '/account/update' => 'firefighters#account_update'
get '/account/change_password' => 'account#change_password'
get '/account/change_cell' => 'account#change_cell'
get '/account/change_email' => 'account#change_email'
get '/account/change_notifications' => 'account#change_notifications'
post '/account/update_cell' => 'account#update_cell'
# post '/account/update_pass' => 'account#update_pass'
# post '/account/update_cell' => 'account#update_cell'
# post '/account/update_email' => 'account#update_email'
# post '/account/update_notifications' => 'account#update_notifications'
# get '/account/change_cell_number' => 'firefighters#account_change_cell_number'
# post '/account/request_cell_change' => 'firefighters#account_request_cell_change'
# post '/account/update_cell' => 'firefighters#account_update_cell'
# get '/account/change_password' => 'firefighters#account_change_pass'
# post '/account/update_password' => 'firefighters#account_update_pass'
# get '/account/change_email' => 'firefighters#account_change_email'
# post '/account/request_email_change' => 'firefighters#account_request_email_change'
# post '/account/update_email' => 'firefighters#account_update_email'
get '/approve' => 'firefighters#approve'
get '/set_tester' => 'firefighters#set_tester'
end
scope :entries do
post '/verify' => 'entries#verify'
post '/approve' => 'entries#approve'
post '/finalize' => 'entries#finalize'
post '/comment' => 'entries#comment'
post '/update' => 'entries#update'
get '/edit/:entry_id' => 'entries#edit'
end
I used scope
to avoid writing a million routes. I moved Entry
routes under resources :entries
for this reason.
Currently, every user type has their own dashboard page with slight variations, just to make it work
Right now there is a separate dashboard.slim
, account.slim
, etc for Firefighters, Chiefs, you get it.
How I'd like things to be
I'd like to have one dashboard to rule them all. However links like firefighters_dashboard_path
isn't terribly useful if you only have one view file. I work around this problem by having a slightly different dashboard page for each user. This seems wrong and not at all DRY.
How do I organize routes so that there is one dashboard and depending on the user, links like account_path
would lead one to firefighter's account and not an admin's account, for instance?
In other words, I login as captain, the link to my account should be site.com/account
. I login as manager, my link to management should be 'site.com/managing'. Saving "identity" in session is craziness.
This is a bit to take in, apologies for the wall of text but help would be much appreciated.
来源:https://stackoverflow.com/questions/36603886/consolidating-messy-routes-in-rails-4-how-to-organize-routes-to-not-point-urls