Topic: form_for routing problem

Hi

I've got an item has_one description relationship however a generated route isn't behaving as I'd expect.

##routes.rb
map.resources :items, :has_one => :description
##

## generated routes
item_description GET             /items/:item_id/description/new    {:action=>"new", :controller=>"descriptions"} ## not interested in this
edit_item_description GET    /items/:item_id/description/edit    {:action=>"edit", :controller=>"descriptions"}
item_description GET             /items/:item_id/description            {:action=>"show", :controller=>"descriptions"}
##

## item model
has_one :description, :as => :owner, :dependent => :destroy
##

##description model
belongs_to :owner, :polymorphic => true
##

## descriptions controller
def edit
  @item = Item.find(params[:item_id])
  @description =   @item.description ||= @item.build_description
end
##

## edit template
<% form_for [@item, @description] do |form| %>
##

## error
undefined method `item_descriptions_path' for #<ActionView::Base:0x2543cb8>
##


As you can see it is wrongly generating the plural form item_descriptions_path. This occurs on a new description though not if I create one in console and save it - in which case the form generates ok. I wasn't planning on using a new method as whether the description is new or not seems unimportant from the point of view of the user. Oh, and the description isn't in the item model due to it not always existing.

Using rails 2.3

Thanks in advance for great words of wisdom!

smile

Last edited by mark_d (2009-04-01 04:58:26)

Re: form_for routing problem

while i can't tell you why this is happening (it shouldn't, and i don't see any errors on your part), this might be a workaround in case you have to fix this asap:

<% form_for @description, :url => item_description_path(@item) do |form| %>

Re: form_for routing problem

I hate to ask a stupid question and all wink, but look on the page that the form itself is placed on new.html.erb, or whatever it is. There should be some link or reference on that page. Does it reference new_description_path, or new_descriptions_path?

Re: form_for routing problem

Thanks both for replies.

Duplex the workaround works fine. I hope to figure this out as it's a project for uni where I will demonstrate rails features.

Puzzling, but I'm kinda happy at least not to have missed something obvious (this time).

smile

Last edited by mark_d (2009-04-01 05:34:20)

Re: form_for routing problem

Having found this I'm guessing this will be fixed fairly soon.

https://rails.lighthouseapp.com/project … d-resource

The second comment describes my problem. Is there somewhere I can better check status of this?

Re: form_for routing problem

Ok so everything I previously posted was in haste, quick testing proved my jubilation ill founded. It does appear there exists a bug with polymorphic_url giving an incorrect url for a has_one nested resource.

Hence I see that:

<% form_for [@item, @description] do |form| %>
# results in
<form id="edit_description_18" class="edit_description" method="post" action="/items/7/description.%23%3Cdescription:0x245be18%3E">

The nonsense appended to the end of the form action is what's causing the problem.

Last edited by mark_d (2009-04-01 05:20:59)

Re: form_for routing problem

Please see previous edited post.

How can I check if or when edge rails fixes this?

Last edited by mark_d (2009-04-01 05:33:04)

Re: form_for routing problem

Just curious, does using this work:

form_for [@item, :description]

It should generate the correct route, I'm really just interested to know whether rails automatically populates the form using the object on the association (i.e. @item.description).

If I've helped you out with something, feel free to recommend me on working with rails.

Re: form_for routing problem

Hi and thanks for reply.

Yes and no. It generates the url fine, actually I use this elsewhere to create a link to the edit action:

<%= link_to 'Edit Description', edit_polymorphic_path( [@item, :description] ) -%>

However in the context of form_for using :description brings up undefined method errors for the form fields, as you predicted.

Last edited by mark_d (2009-04-01 06:00:14)

Re: form_for routing problem

Of which class is the object causing Undefined method errors an instance? Item/Description/nil?

If I've helped you out with something, feel free to recommend me on working with rails.

Re: form_for routing problem

undefined method `body' for :description:Symbol

(Body is a field of the description model, think body of text)

Re: form_for routing problem

Okay. Looks like there's no hope there then. sad

If I've helped you out with something, feel free to recommend me on working with rails.

Re: form_for routing problem

Yeah, seems definitely to be a bug as described here. I've got a couple of months before I have to make this demonstration so hopefully it'll be fixed before then.

Thanks very much for your help. smile

Last edited by mark_d (2009-04-01 06:13:41)

Re: form_for routing problem

You can't be held accountable for a bug in Rails, so I wouldn't worry about it if it's not fixed come presentation day.

Now, if you were to submit a patch, that seems like something that would certainly be worth talking about, and you'll be able to join all the "I contributed to an open source project" kids.

If I've helped you out with something, feel free to recommend me on working with rails.

Re: form_for routing problem

If only I had the time and expertise. sad

Last edited by mark_d (2009-04-01 06:24:56)