2012-11-04 20 views
0

我有一個小型的Ruby on Rails應用程序,我試圖重構,並且在這個過程中已經打破了保存一些數據的能力。我花了整天試圖解決這個問題(我是一個Rails新手,基本上只限於嘗試改進我已經有的應用程序)。在Ruby on 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) 
+0

嘗試在某處產生錯誤。它在開發模式中應該足夠冗長。 – Reactormonk

+0

顯示LocationsController和發佈到創建操作的日誌輸出會很有幫助。 – rossta

+0

感謝您的評論 - 我已經在issue中發佈了來自development.log的輸出。這有幫助嗎? –

回答

0

我看到,爲位置生成的資源路由包括其必須存在一個DOCUMENT_ID。我假設你正在從文檔路線定義位置資源路線作爲嵌套。因此:

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

看起來不完整,因爲您沒有將文檔傳遞給路由。嘗試:

<%= form_for(@document, @location) do |f| %> 
+0

謝謝ChuckE。這將返回錯誤的參數數量(1爲0)' –

+0

抱歉,不好的電話,我的意思是form_for(@location)do | f | (post和url被推斷出來,但是這並不能解決你的問題,發佈日誌錯誤,plz,這樣很難 – ChuckE

+0

就像上面那樣改變了,並且對於#<#:0x00000004a80d28>'我在哪裏可以找到日誌錯誤?此刻生成的唯一日誌是development.log –