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

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
Please log in before posting. Registration is free and takes only a minute.
Existing account

NEW: Do you have a Google/GoogleMail, Yahoo or Facebook account? No registration required!
Log in with Google account | Log in with Yahoo account | Log in with Facebook account
No account? Register here.