How can I get rails routes to match * for the fina

2019-08-21 09:41发布

问题:

I would like to allow a user to automatically login if they use their encrypted password in the url. I have the following url that I would like matched with rails routes:

/attendants/update_player_status/game/1/maybe/user_hash/$2a$10$1kY8xvOjkqZJJJGWDd0q2Oeyl7kICSBDPTYzGhkJ6pZnE6YA/nJie

where the latter part of the url can have any characters, like * (ie. it can have '/' '$' '*' etc)

My routes works but then it bombs out on '/' in the parameter (as seen above):

match '/attendants/update_player_status/game/:game_id/:status/user_hash/:user_hash' => 'attendants#update_player_status_using_hash', :as => 'update_player_status_using_hash'

Any recommendations on what I should do? Once i pull the parameter correctly I will use it to login the user into the website, and allow very limited access.

回答1:

One option is route globbing (from the docs):

match 'photos/*other' => 'photos#unknown'

# In controller, params[:other] is everything after 'photos'

You could also use a regex in the match's constraints to allow a slash, as per this answer.

IMO the globbing is a bit more elegant; it may depend on the rest of your routes which is the best solution.