2011-04-02 36 views
1

我正在通過Mongoid玩Rails 3中的MongoDB。我已經定義了下面的類,但是當我嘗試通過腳手架視圖創建一個新的超鏈接時,我得到一個錯誤。我相信發生的事情是Tags陣列沒有被正確處理。我正在使用默認的控制器腳手架。我需要做些什麼來確保mongoid知道如何添加標籤?關於mongoid references_many,視圖和控制器的問題

class Hyperlink 
    include Mongoid::Document 
    field :name 
    field :url 
    embeds_many :comments 

    references_many :tags 

    validates_presence_of :name, :url 
    validates_uniqueness_of :name, :url 
end 

class Tag 
    include Mongoid::Document 

    field :name 
    validates_uniqueness_of :name 
    referenced_in :hyperlink  
end 

控制器響應 類型錯誤在HyperlinksController#創建

can't convert Symbol into Integer 

**Request** 

Parameters: 

{"utf8"=>"✓", 
"authenticity_token"=>"yn5SwZPBIMcpzrGQeO9t3tJ2Y2Q6nlsDBPbI43ahj0k=", 
"hyperlink"=>{"name"=>"Stack Overflow", 
"link"=>"http:://www.stackoverflow.com", 
"tags"=>{"tag"=>"programming"}}, 
"commit"=>"Create Hyperlink"} 
+0

請在這裏張貼控制器和查看源代碼或作爲要點。 (或者,將整個shebang推送到GitHub項目。) – jdc 2011-04-05 16:09:04

回答

2

嘗試添加accepts_nested_attributes_for :tagsHyperLink

+0

就是這樣!限制解除時,我會盡快給予賞金。謝謝! – nathan 2011-04-06 02:21:06

+0

很高興幫助。我無法通過ActiveRecord和Mongoid來告訴你我遇到過同樣的問題。 – bowsersenior 2011-04-06 03:40:52

0

理想的情況下你的參數應該是這個樣子(請注意標籤的變化):

Parameters: 

{"utf8"=>"✓", 
"authenticity_token"=>"yn5SwZPBIMcpzrGQeO9t3tJ2Y2Q6nlsDBPbI43ahj0k=", 
"hyperlink"=>{"name"=>"Stack Overflow", 
"link"=>"http:://www.stackoverflow.com", 
"tags"=>{"0" => {"name"=>"programming", :id => "xxxx"}}}, 
"commit"=>"Create Hyperlink"} 

正如JDC已經指出控制器並且查看代碼對於實際指出問題會很有幫助。