form_for examples

35 views
Skip to first unread message

Karl Shea

unread,
Jul 20, 2009, 5:52:29 PM7/20/09
to resource_controller
Could you provide some documentation on how form_for statements should
look like? It seems like what's expected keeps changing and I'm having
a hard time keeping up with what style I should be using.

For example, this used to work for the new action (this is a nested
resource):

form_for(:ticket, :url => object_url(@ticket), :html => { :multipart
=> true }) do |f|

Under Rails 2.3.2 and resource_controller 0.6.5, it fails with the
error

I finally got it to work with "project_ticket_url failed to generate"

form_for(@ticket, :url => collection_path, :html => { :multipart =>
true }) do |f|

Is that actually correct, or is Rails 2.3.2 not supported yet?

Karl Shea

unread,
Jul 21, 2009, 5:17:32 PM7/21/09
to resource_controller
Oops, a little typo there.

I tracked it down a little further:

object_url(@ticket) is trying to generate the URL project_ticket_url
and pass it @project and @ticket, which looks like it's failing since
Rails is trying to parse @ticket as a format now.

But I'm not sure even where to go with this... I can't just do form_for
(@ticket) because that doesn't pass in the correct association, and
while form_for([@project, @ticket]) works, what am I supposed to do
when it's polymorphic and I don't know what the parent object is?

Is the correct way

form_for(:ticket, :url => collection_path, :html => { :method
=> :post })

?

It just doesn't seem right.

Any thoughts?

Karl Shea

unread,
Jul 22, 2009, 4:15:28 PM7/22/09
to resource_controller
Well, it seems like

form_for([parent_object, @ticket])

works just fine, for anyone coming across the same problem.

James Golick

unread,
Jul 22, 2009, 4:17:38 PM7/22/09
to resource_...@googlegroups.com
Hi Karl,

The correct syntax, using resource_controller's url helpers to keep DRY in polymorphic situations would be form_for :url => collection_url. The rails convention is that POST to /plural is create. So, the collection_url is correct in this case.

- J.

Peter Jaros

unread,
Jul 22, 2009, 4:19:43 PM7/22/09
to resource_...@googlegroups.com
On Wed, Jul 22, 2009 at 4:15 PM, Karl Shea<karl...@gmail.com> wrote:
>
> Well, it seems like
>
> form_for([parent_object, @ticket])
>
> works just fine, for anyone coming across the same problem.

Good to know. BTW, you should also be able to say:

form_for([parent_object, object])

if you prefer.

Peter

goodwill

unread,
Sep 3, 2009, 4:16:45 AM9/3/09
to resource_controller
Ok now I see people are saying collection_url is for create, but
combining with form_for is a problem. The form_for helper was smart
enough to check if you have a new_record object to actually generate
the correct url (collection_url or object_url), while it gives you the
annoyance of typing the full nested object list in array. Now we try
to use Urligence to save the hassle on typing the full list, while now
I have to determine if I have to use collection_url or object_url, not
really nice to be honest.

On Jul 23, 4:19 am, Peter Jaros <peter.a.ja...@gmail.com> wrote:

Yeung William

unread,
Sep 3, 2009, 4:39:17 AM9/3/09
to resource_...@googlegroups.com
I end up writing this as a helper:

def form_smart_url(object=nil)
object=@object if object.nil?
if object.respond_to?(:new_record?)
object.new_record? ? collection_url : object_url(object)
else
object_url(object)
end

end


Yeung William
william....@gmail.com
Reply all
Reply to author
Forward
0 new messages