2011-03-27 24 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

相关问题