views:

193

answers:

1

In Rails 3 edge, I have set up two nested resources like this:

config/routes.rb

resources :agencies do
   resources :properties
end

I use Mongomapper as my ORM, and when I try to create a new property under an existing agency, I get this error:

---> http://localhost:3000/agencies/4c3ff0f455899f0fb5000001/properties/new

NoMethodError in Properties#new

Showing /Users/peter/programming-MacBookPro/iPhone/inspector-on-rails/app/views/properties/_form.html.erb where line #1 raised:

undefined method `properties_path' for #<#<Class:0x00000100dae020>:0x00000100d97f00>
Extracted source (around line #1):

1: <%= form_for @property do |f| %>
2: <% if @property.errors.any? %>
3:    <div id="error_explanation">
4:      <h2><%= pluralize(@property.errors.count, "error") %> prohibited this property from being saved:</h2>
Trace of template inclusion: app/views/properties/new.html.erb

Rails.root: /Users/peter/programming-MacBookPro/p-on-rails

Application Trace | Framework Trace | Full Trace
app/views/properties/_form.html.erb:1
app/views/properties/new.html.erb:3
app/controllers/properties_controller.rb:29:in `new'
Request

Parameters:

{"agency_id"=>"4c3ff0f455899f0fb5000001"}

In properties_controller.rb:

def new
    @property = Property.new

    respond_to do |format|
      format.html # new.html.erb
      format.json  { render :json => @property }
    end
end

in app/views/properties/_form.html.erb:

<%= form_for @property do |f| %>
<% if @property.errors.any? %>
   <div id="error_explanation">
     <h2><%= pluralize(@property.errors.count, "error") %> prohibited this property from being saved:</h2>

     <ul>
     <% @property.errors.full_messages.each do |msg| %>
       <li><%= msg %></li>
     <% end %>
     </ul>
   </div>
 <% end %>
 <div class="field">
     <%= f.label :address_postcode %><br />
     <%= f.text_field :address_postcode %>
   </div>
  <div class="actions">
    <%= f.submit %>
  </div>
<% end %>

In app/views/properties/new.html.erb:

<h1>New property</h1>
<%= render 'form' %>
<%= link_to 'Back', @property %>

I suspect that the error about properties_path not being defined has something to do with the nesting of the properties resource under agencies. However, I can't figure out how to deal with the complain in line 1 of _form.html.erb as something like form_for (@agency, @property) doesn't work.

Any ideas?

+1  A: 

according to your route, property is nested under agency, right?

to avoid errors, you can follow this way:

step1- in controller, in 'new' action, instantiate @property object as:

@property = Property.new(:agency_id => params[:agency_id]) # this will have agency association from beginning

step2- in _form.html.erb use this:

form_for (@property.agency,@property)

this code should work for sure (I use it often), don't know if there's a cleaner/less-ugly solution ;)

apeacox
Thanks, that worked. I found also that various url helpers are also generated, like edit_agency_property_path(@agency, @property), etc. Thank for your help.
futureshocked