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_fo开发者_如何学Pythonr'
<% 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
You can use
<%= form_for [@website, @domain] %>
Where @domain
belongs to @website
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| %>
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...
精彩评论