2012-05-19 20 views
1

與此問題相關的模型:user,friend,interest,person_interest。 Person_interest是polymoprhic,可以存儲屬於用戶或朋友的興趣。Rails應用中的嵌套表單錯誤

興趣預先填入約。 30興趣。當用戶註冊時,他們被要求選擇自己的興趣,然後在表格中輸入他們的朋友的名字來選擇該人的興趣。

試圖與<%= friend_f.input :interests, :as => :check_boxes, :label => false %>

來實現這一點,但得到以下錯誤:

NoMethodError in User_steps#show 

Showing /Users/nelsonkeating/Desktop/ReminDeal/app/views/user_steps/show.html.erb where line #30 raised: 

undefined method `to_i' for []:ActiveRecord::Relation 
Extracted source (around line #30): 

27:   :end_year => Date.today.year - 12, 
28:   :order => [:month, :day, :year ] %> 
29:  <%= friend_f.input :gender, :collection => ['male','female'] %> 
30:  <%= friend_f.input :interests, :as => :check_boxes, :label => false %> 
31:  <%end%> 
32: 
33: 
Rails.root: /Users/nelsonkeating/Desktop/ReminDeal 

Application Trace | Framework Trace | Full Trace 
app/views/user_steps/show.html.erb:30:in `block (2 levels) in _app_views_user_steps_show_html_erb__1202886937753978667_70281885932700' 
app/views/user_steps/show.html.erb:23:in `block in _app_views_user_steps_show_html_erb__1202886937753978667_70281885932700' 
app/views/user_steps/show.html.erb:1:in `_app_views_user_steps_show_html_erb__1202886937753978667_70281885932700' 
Request 

Parameters: 

{"id"=>"show"} 

而這裏的形式..

<%= simple_form_for @user do |f| %> 

    <%= f.input :name %> 
    <%= f.input :city %> 
    <%= f.input :address, :live => true %> 
    <%= f.input :zipcode %> 
    <%= f.input :date_of_birth, :as => :date, :start_year => 1900, 
    :end_year => Date.today.year - 12, 
    :order => [ :day, :month, :year] %> 
    <%= f.input :gender, :collection => [:male, :female] %> 


    <h4>Select your top 3 interests..</h4> 
     <%= f.association :interests, :as => :check_boxes, :label => false %> 

    <h4>What holidays do you celebrate?</h4> 
     <%= f.association :holidays, :as => :check_boxes, :label => false %> 



<h4>Add up to 10 friends birthdays that you would like to remember..</h4> 
    <%= f.simple_fields_for :friends do |friend_f| %> 

     <%= friend_f.input :name %> 
     <%= friend_f.input :dob, :label => :Birthday, :as => :date, :start_year => Date.today.year - 90, 
     :end_year => Date.today.year - 12, 
     :order => [:month, :day, :year ] %> 
     <%= friend_f.input :gender, :collection => ['male','female'] %> 
     <%= friend_f.input :interests, :as => :check_boxes, :label => false %> 
    <%end%> 


<%= f.button :submit, :class => 'btn btn-success' %> 
     <%end%> 

的Git回購是在這裏:https://github.com/nelsonkeating/ReminDeal

+0

所以它的這一行 「<%= friend_f.input:利益:如=>:check_boxes,:標籤=>虛假%>」 ... – js111

回答

6

你有什麼理由,以避免friend_f.association

<%= friend_f.association :interests, :as => :check_boxes, :label => false %> 

適合我。 或更改:interests:interest_ids卡斯帕的代碼,也應該工作。

<%= friend_f.input :interest_ids, :as => :check_boxes, :label => false, :collection => Interest.all %> 
+0

哇。我是慢熱型。謝謝! – js111

+0

我學到了新東西:D –

0

嘗試編寫代碼<%= friend_f.input :interests, :as => :check_boxes, :label => false %> w ith :collection鍵。 例如:

<%= friend_f.input :interests, :as => :check_boxes, :label => false, :collection => Interest.all %> 
+0

使我有以下錯誤:「NameError在User_steps #show .....未初始化的常量ActionView :: CompiledTemplates :: Interests「 – js111

+1

在kasper的代碼中,將':collection => Interests.all'更改爲':collection => Interest.all'並嘗試。該模型應該是單一的。 –

+0

哦,我很愚蠢。謝謝,@StephRose – kasper375