我正在做我的第一个 Rails 项目,我有以下模型关系:
class Profile < ActiveRecord::Base
belongs_to :identifiable, polymorphic: true
accepts_nested_attributes_for :students
class Student < ActiveRecord::Base
has_one :profile, as: :identifiable
attr_accessible :profile
相关的控制器是:
class StudentsController < ApplicationController
def new
@student = Student.new
end
def create
@student = Student.new(params[:student])
if @student.save
redirect_to root_path
else
render 'new'
end
end
end
和
class ProfilesController < ApplicationController
def new
@profile = Profile.new
end
def create
@profile = Profile.new(params[:profile])
@profile.save
end
end
我正在尝试做的是Student
使用以下形式创建一个新的,它位于students\new.html.erb
:
<h1>Create a new Student Account</h1>
<div class="row">
<div class="span6 offset3">
<%= form_for(@student) do |f| %>
<%= render 'shared/error_messages' %>
<%= f.fields_for :profile, @profile do |builder| %>
<%= builder.label :name %>
<%= builder.text_field :name %>
<%= builder.label :email %>
<%= builder.text_field :email %>
<%= builder.label :password %>
<%= builder.password_field :password %>
<%= builder.label :password_confirmation, "Confirmation" %>
<%= builder.password_field :password_confirmation %>
<% end %>
</div>
</div>
<p><%= f.submit "Submit", class: "btn btn-large btn-primary" %></p>
<% end %>
当我尝试提交表单时收到以下错误消息:No association found for name 'students'. Has it been defined yet?
我做错了什么?提前致谢。