. . . , .
, StudySubject Subject .
study.rb
belongs_to :student, :class_name => "User", :foreign_key => "user_id"
belongs_to :subject
belongs_to :university, :class_name => "Facility", :foreign_key => "facility_id"
accepts_nested_attributes_for :subject, :university
studies_controller.rb
def new
@study = Study.new
@study.subject = Subject.new
@study.university = Facility.new
end
def create
@study = Study.new(params[:study])
@study.student = current_user
if @study.save
flash[:notice] = "Successfully created study."
redirect_to(:action => 'index')
else
render('new')
end
end
devise cancan . current_user .
new.html.erb
<%= form_for @study, :url => { :action => "create" } do |f| %>
<table summary="Study form fields">
<%= render :partial => "shared/study_form_fields", :locals => { :f => f } %>
<%= f.fields_for :subject do |builder| %>
<%= render :partial => "shared/subject_form_fields", :locals => { :f => builder } %>
<% end %>
<%= f.fields_for :university do |builder| %>
<%= render :partial => "shared/facility_form_fields", :locals => { :f => builder } %>
<% end %>
</table>
<p><%= f.submit "Submit" %></p>
<% end %>
, . , , .