Link_to using a hashed :token instead of :id

With Rails 3, how do I create a “link_to” reference that uses a pre-
exisiting token value instead of the record id? Is there a way to use
the URLHelpers to do this, or do I need to use the old style “link_to”
syntax with action, controller, id etc? The documentation doesn’t seem
to provide any pointers that don’t implicitly use :id.

I have a medical database where I’m trying to provide non-trivial
references to patient information. I calculate and store a hashed
token (:token) in the patient’s record. I want to
find_by_token(params[:id]), but I need to tell Rails how to create the
reference using the :token instead of the :id in the view.

Thanks,
Dave

On Nov 9, 3:14am, dwormuth [email protected] wrote:

With Rails 3, how do I create a “link_to” reference that uses a pre-
exisiting token value instead of the record id? Is there a way to use
the URLHelpers to do this, or do I need to use the old style “link_to”
syntax with action, controller, id etc? The documentation doesn’t seem
to provide any pointers that don’t implicitly use :id.

Two ways that I can think of. You can either override the 2 param
method, or if you create a route that looks like /foos/:token then the
corresponding help will expect you to pass a :token option to it.

Fred

reference using the :token instead of the :id in the view.
In your model…

def to_param
code_to_generate_token
end

Once that’s done then…

non_trivial_named_path(@model_instance)

will use the result of to_param automatically.

Or, if you don’t always want it just do…

non_trivial_named_path(@model_instance.token)

And remember that params[:id] is really the token in that case.

-philip