Forum: Ruby on Rails How does rails determine the path from an object?

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Ca94b06e32f746968f0aec970a702a0c?d=identicon&s=25 Jason Waldrip (Guest)
on 2012-11-17 06:30
(Received via mailing list)
I have been working on implementing my own ORM. And I was wondering how
the
rails path helper extracts the ID from the object. For example how would
I
make this work for my ORM?

    @contact = Contact.first
    contact_path(@contact)

Any help would be greatly appreciated!
81b61875e41eaa58887543635d556fca?d=identicon&s=25 Frederick Cheung (Guest)
on 2012-11-17 10:09
(Received via mailing list)
On Nov 17, 5:29am, Jason Waldrip <ja...@waldrip.net> wrote:
> I have been working on implementing my own ORM. And I was wondering how the
> rails path helper extracts the ID from the object. For example how would I
> make this work for my ORM?
>
>   @contact = Contact.first
>   contact_path(@contact)
>
> Any help would be greatly appreciated!

These end up calling polymorphic_path and the magic term here is
active model. Active model is 2 things: reusable modules dealing with
things such as validations, dirty attributes etc. and a protocol that
classes should conform to. It's the latter you're interested in. There
are only a handful of methods you have to implement, including (from
memory) to_param, new_record? And a few others. There's also an
ActiveModel::Lint module that tests that your implementations of these
primitives are valid. Once you have that you should be able to use the
URL helpers, do stuff such as form_for(@contact) and so on

Fred
This topic is locked and can not be replied to.