Jus*_*zer 1 ruby ruby-on-rails nested-attributes ruby-on-rails-3
我有一个has_one配置文件的用户模型,该配置文件属于该用户.该配置文件有一个type用于单表继承的列,可以是"artist"或"listener".我希望用户在新用户视图中注册时设置此项.因此我有以下形式的代码:
<%= 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 %>
Run Code Online (Sandbox Code Playgroud)
这在我的用户模型中:
accepts_nested_attributes_for :profile
Run Code Online (Sandbox Code Playgroud)
但我得到他的错误:
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?
Run Code Online (Sandbox Code Playgroud)
为什么会这样,我该如何解决?
另外我很困惑为什么错误会在我的视频部分中引出第3行,这根本就没有提到profile......
更新:
这是整个表格:
<%= 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 %>
Run Code Online (Sandbox Code Playgroud)
这些是与用户模型中的配置文件相关的三行:
accepts_nested_attributes_for :profile
before_create :build_profile
has_one :profile
Run Code Online (Sandbox Code Playgroud)
你应该编辑你的模型,并按正确的顺序设置所有这些东西:
has_one :profile # it should be first
accepts_nested_attributes_for :profile
before_create :build_profile
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1810 次 |
| 最近记录: |