0

リファクタリングしようとしている小さな Ruby on Rails アプリケーションがあり、その過程で一部のデータを保存する機能が壊れています。私はこれを整理するために文字通り丸一日を費やしました(私はRailsの初心者で、基本的にはすでに持っているアプリを改善しようとすることに制限されています)。

場所が関連付けられているドキュメントがあります。

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

class Location < ActiveRecord::Base
  belongs_to :document
end

ドキュメントに既に追加されている場所を編集できます (これは 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 %>           

ただし、新しい場所を追加しようとしてもエラーにはなりませんが、保存も追加しないでください

<% @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 に答える