1
屬於對方,我不能讓怎麼做的一件事寥寥數款。我有機型用戶,教師,TeacherEducation。 老師教育屬於老師,老師屬於用戶。我想保存用戶,老師和老師教育在我的控制器中的user.save
在一行中。那可能嗎?如果沒有,那麼哪個更好的方法來保存呢?的Rails 3 - 保存它通過嵌套屬性
class User < ActiveRecord::Base
attr_accessible ...,
:teacher_attributes
has_one :teacher
accepts_nested_attributes_for :teacher
end
class Teacher < ActiveRecord::Base
belongs_to :user
has_one :teacher_education
accepts_nested_attributes_for :teacher_education
end
class TeacherEducation < ActiveRecord::Base
belongs_to :teacher
end
現在沒有TeacherEducation我有這樣的說法
<%= form_for @user, :url => create_teacher_url, :html => {:class => "form-horizontal"} do |f| %>
<%= field_set_tag do %>
<%= f.fields_for :teacher do |builder| %>
# Fields of teacher
<% end %>
# Fields of user
<% end %>
我控制器
class AdminsController < ApplicationController
def new_teacher
@user = User.new
teacher = @user.build_teacher
...
end
def create_teacher
user = User.new(params[:user])
if user.save
...
end
end
end