0

I have a small Ruby on Rails application that I am trying to refactor and in the process have broken the abilty to save some data. I have literally spent the entire day trying to sort this out (I am a Rails newbie, basically restricted to trying to improve an app I already have).

I have Documents which have have locations associated with them:

class Document < ActiveRecord::Base   
  has_many :locations, :dependent => :destroy
end

class Location < ActiveRecord::Base
  belongs_to :document
end

I can edit Locations that have already been added to the Document (this is from documents/locations.html):

<% @locations.each do |location| %>
  <% unless location.new_record? %>
    <%@location = location%>
    <%= form_for(@location,:url=>document_location_path(@document,@location),:method=>:put) do |f| %>
      <%= render :partial => "document_locations/form", :locals => {:f => f } %>
    <% end %>
  <% end %> 
<% end %>           

But attempts to add new Locations don't error, but don't add save either

<% @location = @document.locations.new %>
<%= form_for(@location,:url=>:document_locations, :method=>:post) do |f| %>
  <%= render :partial => "document_locations/form", :locals => {:f => f } %>
<%end%>

When I rake routes, I see the following

      document_locations        /documents/:document_id/locations(.:format)                   {:controller=>"documents", :action=>"locations"}

    document_document_locations GET    /documents/:document_id/document_locations(.:format)          {:action=>"index", :controller=>"document_locations"}
                                POST   /documents/:document_id/document_locations(.:format)          {:action=>"create", :controller=>"document_locations"}
 new_document_document_location GET    /documents/:document_id/document_locations/new(.:format)      {:action=>"new", :controller=>"document_locations"}
edit_document_document_location GET    /documents/:document_id/document_locations/:id/edit(.:format) {:action=>"edit", :controller=>"document_locations"}
     document_document_location GET    /documents/:document_id/document_locations/:id(.:format)      {:action=>"show", :controller=>"document_locations"}
                                PUT    /documents/:document_id/document_locations/:id(.:format)      {:action=>"update", :controller=>"document_locations"}
                                DELETE /documents/:document_id/document_locations/:id(.:format)      {:action=>"destroy", :controller=>"document_locations"}

I'm guessing my routes and controllers are in a right old mess, but any suggestions as to how I could start to trouble shoot would be appreciated.

development.log shows this when I attempt to add a location:

Started POST "/documents/210/locations" for 127.0.0.1 at 2012-11-04 23:14:16 +0000
DEPRECATION WARNING: class_inheritable_attribute is deprecated, please use class_attribute method instead. Notice their behavior are slightly different, so refer to class_attribute documentation first. (called from acts_as_taggable_on at /home/christian/Documents/business/development/pastpaper/vendor/plugins/acts-as-taggable-on/lib/acts_as_taggable_on/acts_as_taggable_on.rb:34)
  Processing by DocumentsController#locations as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"OEsBVZPX8NznM0oO/O38/BSrjlpJrNV7oEZTWTcQV9c=", "location"=>{"street1"=>"xian1", "street2"=>"xian2", "town"=>"xian3", "county"=>"", "state"=>"", "country"=>""}, "commit"=>"Save", "document_id"=>"210"}
  Document Load (0.6ms)  SELECT `documents`.* FROM `documents` WHERE `documents`.`id` = 210 LIMIT 1
  DocumentAttribute Load (0.6ms)  SELECT `document_attributes`.* FROM `document_attributes` WHERE `document_attributes`.`document_id` IN (210)
Christian Mayne
  • 1,709
  • 7
  • 26
  • 42

1 Answers1

0

as I see, the generated resource routes for the location comprise a document_id which has to be present. I assume you are then defining the location resources routes as a nesting from the document routes. Therefore this:

<%= form_for(@location,:url=>:document_locations, :method=>:post) do |f| %>

looks incomplete, because you are not passing the document to the route. Try:

<%= form_for(@document, @location) do |f| %>
ChuckE
  • 5,610
  • 4
  • 31
  • 59
  • Thanks ChuckE. That returns `wrong number of arguments (1 for 0)` – Christian Mayne Nov 05 '12 at 00:06
  • sorry, bad call, i meant form_for(@location) do |f| (post and url are inferred. but this does not solve ur issue. post the log error, plz, it's hard this way. – ChuckE Nov 05 '12 at 07:22
  • Just changed as above and had the following error `undefined method `locations_path' for #<#:0x00000004a80d28>`. Where do I find the log error? The only log being produced at the moment is development.log – Christian Mayne Nov 05 '12 at 10:45
  • humm... check this: http://stackoverflow.com/questions/2034700/form-for-with-nested-resources . I don't know how you defined the routes, but one of the variants should have worked... – ChuckE Nov 05 '12 at 13:11