views:

235

answers:

3

I am trying to create a form for a nested resource. Here is my route:

map.resources :websites do |website|
  website.resources :domains
end

Here are my attempts and the errors:

<% form_for(@domain, :url => website_domains_path(@website)) do | form | %>
<%= form.text_field :name %>

# ArgumentError: wrong number of arguments (1 for 0)
# form_helper.rb:290:in 'respond_to?'
# form_helper.rb:290:in 'apply_form_for_options!'
# form_helper.rb:277:in 'form_for'

<% form_for([@website, @domain]) do | form | %>
<%= form.text_field :name %>

# ArgumentError: wrong number of arguments (1 for 0)
# form_helper.rb:290:in 'respond_to?'
# form_helper.rb:290:in 'apply_form_for_options!'
# form_helper.rb:277:in 'form_for'

<% form_for(:domain, @domain, :url => website_domains_path(@website)) do | form | %>
<%= form.text_field :name %>

# ArgumentError: wrong number of arguments (1 for 0)
# wrapper.rb:14:in 'respond_to?'
# wrapper.rb:14:in 'wrap'
# active_record_helper.rb:174:in 'error_messages_for'

<% form_for(:domain, [@website, @domain]) do | form | %>
<%= form.text_field :name %>

# UndefinedMethodError 'name' for #<Array:0x40fa498>

I have confirmed both @website and @domain contain instances of the correct class.

The routes also generate correctly is used like this for example, so I dont think their is an issue with the route or url helpers.

<%= website_domains_path(1) %>
<%= website_data_source_path(1, 1) %>

Rails 2.3.5

A: 

I've just tried:

<% form_for @note, :url => teams_person_notes_path(@person) do |f| %>
  <%= f.text_field :note %>
  <%= f.submit "Add" %>
<% end %>

and it works without any problem. My routes looks like this:

  map.namespace :teams do |t|
    t.resources :people do |p|
      p.resources :notes
    end
  end

So it is the same as yours (only namespace is added, but it's not a case).

Of Course, your example form is for new object of @domain. It won't work for edit action, then you should have:

 <% form_for @domain, :url => edit_website_domain_path(@website, @domain) do |form| %>
klew
This could be a deeper problem than I first thought. The form example is for a new object, ie. @domain.new_record? # => true.
Kris
I think that your second example also should work, but both for new and edit. So probably you have error somewhere else. Do you have all blocks closed with `end`? Or, try to remove all fields from form.
klew
A: 

This was so obvious when it finally clicked, I had a field called respond_to. It was of course overriding an existing method.

Its a shame ActiveRecord does not warn when a database column will overwrite an already defined method...

Kris
A: 

You can use

<% for_for [@website, @domain] %>

Where @domain belongs to @website

Hock
Even though the solution to my problem it is the correct answer to the question and I would prefer to award the points to someone other than myself!
Kris