2011-03-27 41 views
1

我有一個has_one配置文件的用戶模型,並且該配置文件屬於該用戶。該配置文件有一個用於單表繼承的type列,可以是「藝術家」或「偵聽器」。我希望用戶在新用戶視圖中註冊時進行設置。因此,我必須在形式驗證碼:在另一個模型的創建視圖中設置一個模型的屬性

<%= f.fields_for :profile do |t| %> 
<div class ="field"> 
    <%= t.label :type, "Are you an artist or listener?" %><br /> 
    <%= t.radio_button :type "artist" %> 
    <%= t.radio_button :type "listener" %> 
    </div> 
<% end %> 

,這在我的用戶模型:

accepts_nested_attributes_for :profile 

,但我得到了自己的錯誤:

ArgumentError in Videos#index 

Showing /rubyprograms/dreamstill/app/views/videos/_video.html.erb where line #3 raised: 

No association found for name `profile'. Has it been defined yet? 

這是爲什麼,以及如何能我修復它?

而且我很困惑,爲什麼錯誤在我的視頻部分,沒有提及profile都帶來了3號線...

UPDATE:

這裏就是整個形式:

<%= form_for(@user) do |f| %> 
    <% if @user.errors.any? %> 
    <div id="errorExplanation"> 
    <h2><%= pluralize(@user.errors.count, "error") %> prohibited this user from being saved:</h2> 
    <ul> 
    <% @user.errors.full_messages.each do |msg| %> 
     <li><%= msg %></li> 
    <% end %> 
    </ul> 
    </div> 
    <% end %> 

    <div class="field"> 
    <%= f.label :email %><br /> 
    <%= f.text_field :email %> 
    </div> 
    <div class="field"> 
    <%= f.label :password %><br /> 
    <%= f.password_field :password %> 
    </div> 
    <div class="field"> 
    <%= f.label :password_confirmation, "Password Confirmation" %><br /> 
    <%= f.password_field :password_confirmation %> 
    </div> 
    <%= f.fields_for :profile do |t| %> 
    <div class ="field"> 
     <%= t.label :type, "Are you an artist or listener?" %><br /> 
     <%= t.radio_button :type "artist" %> 
     <%= t.radio_button :type "listener" %> 
    </div> 
    <% end %>  
    <div class="field"> 
    <%= f.label :name, "Full Name" %><br /> 
    <%= f.text_field :name %> 
    </div> 
    <div class="action"> 
    <%= f.submit %> 
    </div> 
<% end %> 

這些都是相關的用戶模型配置文件的三條線:

accepts_nested_attributes_for :profile 
before_create :build_profile 
has_one :profile 
+0

顯示1.用戶模型,2整個表單 – fl00r 2011-03-27 21:39:17

+0

應聲明'HAS_ONE:順便說profile'第一 – fl00r 2011-03-27 21:46:42

回答

5

你應該修改你的模型,並設置所有這些東西在正確的順序:

has_one :profile # it should be first 
accepts_nested_attributes_for :profile 
before_create :build_profile 
+0

...的您的上一個答案的單選按鈕沒有出現在表單中由於某種原因...他們的html不存在 – 2011-03-27 21:52:04

+0

您忘記了逗號'<%= t.radio_button:type,'artist'%>''between':type '和'藝術家' – fl00r 2011-03-27 21:53:24

+0

是啊,我真的把它放進去了,按鈕還是沒有出現......這很奇怪 – 2011-03-27 21:54:14

相關問題