Jason Waldrip
Jason Waldrip

Reputation: 5148

How does rails determine the path from an object?

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!

Update:

My object does have an ID attribute and responds to it. But yet the path helper returns an error.

Upvotes: 0

Views: 161

Answers (2)

Frederick Cheung
Frederick Cheung

Reputation: 84114

In a nutshell you want to be activemodel compliant. This will make url helpers, form_for(@contact) and so on work.

You also get to (optionally) use a bunch of modules dealing with things such as validations, dirty attributes etc.

There are only a handful of methods you have to implement. There's also an ActiveModel::Lint module that tests that your implementations of these primitives are valid, and which also serves as documentation. In particular you need to implement to_param and persisted?. I think some of the naming stuff only gets used if you do stuff like link_to 'foo', @contact

Upvotes: 1

user229044
user229044

Reputation: 239291

The method checks to see if you've passed it an object, or an integer. If it's an object and that object has an id method (respond_to?(:id)), it uses its ID. Pretty dead simple.

Upvotes: 0

Related Questions