2013-03-29 53 views
0

我遇到了使用Mongoid的grouped_collection_select問題。我得到的錯誤消息是:ActionView :: Template :: Error(未定義的方法`合併':name:Symbol)。Rails grouped_coolection_select使用Mongoid

我正在使用rails 3.2.12 &最新版本的Mongoid。

這裏是我的代碼:

class States 
    include Mongoid::Document 
    field :_id 
    field :name 
    field :abbreviation 
    field :countryId 

    belongs_to :countries 
end 

class Countries 
    include Mongoid::Document 
    field :_id 
    field :name 
    field :abbreviation 

    has_many :states 
end 

<div class="field"> 
    <%= f.label :_id, "Country" %><br /> 
    <%= f.collection_select :_id, Countries.order_by([:name, :asc]), :_id, :name, include_blank: true %> 
</div> 
<div class="field"> 
    <%= f.label :_id, "State or Province" %><br /> 
    <%= f.grouped_collection_select(:states, :countryId, Countries.order_by([:name, :asc]), :states, :name, :id, :name) %> 
</div> 

我希望得到任何幫助解決這一問題。

+0

試試這個領域的列名:'<%= f.grouped_collection_select(:STATE_ID,Countries.order_by ([:name::asc]),::states,:name,:id,:name)%> – codeit

回答

0

您正在使用f.grouped_collection_select這意味着第一個參數將從f.object推斷出來。刪除您的第一個參數,並改變第二到你希望它被保存到應該解決您的問題

<%= f.grouped_collection_select(:state, Countries.order_by([:name, :asc]), :states, :name, :id, :name) %> 
+0

感謝您的快速響應。我實現了你的改變,但現在我得到了這個錯誤信息:ActionView :: Template :: Error(未定義的方法'狀態'爲#<設備:0x007fcdc29bf798>)。有關如何解決的任何想法?任何幫助,將不勝感激。 –

+0

嘗試代替:state,:在你的代碼 – regmiprem

+0

中的狀態,你需要在我想要保存選定狀態的字段名的回答中更改':state'。沒有任何你使用表格的知識,我們不會知道字段名稱是什麼。 – jvnill