0

我有一个小型 Rub​​y on Rails 应用程序,我正在尝试重构它,并且在此过程中破坏了保存一些数据的能力。我确实花了一整天的时间试图解决这个问题(我是一个 Rails 新手,基本上仅限于尝试改进我已经拥有的应用程序)。

我有与它们相关联的位置的文档:

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

class Location < ActiveRecord::Base
  belongs_to :document
end

我可以编辑已添加到文档中的位置(来自文档/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 %>           

但是尝试添加新位置不会出错,但也不要添加保存

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

当我耙路线时,我看到以下内容

      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"}

我猜我的路线和控制器处于正确的旧混乱状态,但任何关于我如何开始排除故障的建议将不胜感激。

development.log 在我尝试添加位置时显示了这一点:

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)
4

1 回答 1

0

如我所见,为该位置生成的资源路由包含一个必须存在的 document_id。我假设您随后将位置资源路由定义为文档路由的嵌套。因此:

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

看起来不完整,因为您没有将文档传递给路线。尝试:

<%= form_for(@document, @location) do |f| %>
于 2012-11-04T23:33:25.450 回答