How can I parse the params from a custom URL?
Lets say I have a User class that implements a to_param and a from_param method and I have a backend admin where a "customer" can insert an URL from a user (profile) page (e.g. http://localhost:3000/users/JohnDoe-123security456-123 where 123 is the ID).
Is it possible to generate a custom params object or something similar to parse the id from the url? My goal is to reuse the existing logic instead of creating another regex.
I know I could do something like: "http://localhost:3000/users/JohnDoe-123security456-123/custom_action?abc=def".gsub(/^.*\/users\//, '').gsub(/\/.*$/,'') (or something more suffisticated) to get the id.
Here is the pseudocode of what I try to achieve.
class User < ActiveRecord::Base
def to_param
"#{name}-#{security-token}-#{id}"
end
def self.id_from_param(param_id)
param_id.to_s.gsub(/.*-/,'')
end
end
class AdminUserController < ActionController::Base
def search
url = params[:url]
parsed_params = some_method_that_extracts_the_params_from_url(url) # (1)
user_id = User.id_from_param(parsed_params[:id])
end
end
user_id = User.id_from_param(params[:id])whereparams[:id] == "JohnDoe-123security456-123".http://localhost:3000/admin/search?url=http://localhost:3000/users/JohnDoe-123security456-123. This way I do not have an ID.