views:

320

answers:

2

I'm going to head off and look at the source to see if I can find what's causing this, but someone here's probably run into this before so... I'm doing a normal form_for:

<% form_for(@myobj) do |f| %>

But the URL it's generating is:

form action="/myobjs/%23%3CMyobj:0x105f03ec0%3E" class="edit_myobj" id="edit_myobj_13" method="post">

When it should of course be generating the action "/myobjs/13" if the id==13. I've verified that the object is returning its id correctly, and of course form_for is actually setting the form's id appropriately using the id (as shown above), so... I'm not sure yet what form_for uses to generate the action URL for the form. Anyone out there run into this before and have a solution?

Thanks in advance...

A: 

Do you have your routes setup for the model?

Toby Hede
Yes, sorry, I should have included that originally. Standard RESTful routing:map.resources :myobjs
Masonoise
A: 

I have only had this happen when trying to make a form for a new object, like <% form_for(Widget.new) do |f| %>. Try overriding the to_param method in your model to see if you can find out what's going on.

def to_param
  (id = self.id) ? id.to_s : 'NO ID FOUND! Dig for bugs!'
end

Since you don't inherit from ActiveRecord::Base, you are going to have to implement to_param yourself. The source code for ActiveRecord's to_param is as follows:

def to_param
  # We can't use alias_method here, because method 'id' optimizes itself on the fly.
  (id = self.id) ? id.to_s : nil # Be sure to stringify the id for routes
end

And should work in your situation.

Benjamin Manns
Thanks for this thought, I hadn't gotten there yet. Mysteriously, if I have the to_param() as shown here, the action is generated correctly with action="/myobjs/13". If I comment out the to_param() method then it's generated incorrectly again. Odd, I haven't had this happen before. I'll have to research what to_param() looks like in my other models and superclasses -- this isn't an ActiveRecord model, so its super is simply Object...
Masonoise
ActiveRecord::Base is where to_param is defined, so Object will not generate the method. To fix the error, you will either have to inherit ActiveRecord::Base or add the to_param manually. Simply replace 'NO ID FOUND...' with nil.
Benjamin Manns