我是ruby on rails的新手。我有两个模型一个是课程,另一个是部分。 课程模式 course.rb
class Course < ActiveRecord::Base
attr_accessible :course_name, :school_id
#associations
has_many :sections
end
section.rb
class Section < ActiveRecord::Base
attr_accessible :section_name, :school_id, :course_id
#associations
belongs_to :course
end
* sections_controller.rb *
def new
@section = Section.new
if params[:standard].present?
@course = Course.where(:school_id => params[:standard])
render :partial => "section"
end
end
* _ * form.html.erb
<%= simple_form_for @section, :html => { :class => 'form-horizontal well' } do |f| %>
<div class="input string required"><label class="string required control-label" for="student_id"><abbr title="required">*</abbr> School Name</label><%= f.select :school_id, School.all.map { |i| [i.school_name, i.id]},:prompt => "--Select a Class--" %></div><br/>
<div id = "section_div">
</div>
<!-- Button to trigger modal -->
<a href="#myModal" role="button" class="btn btn-success" data-toggle="modal">Add Section</a>
<!-- Modal -->
<div id="myModal" class="modal hide fade" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
<h3 id="myModalLabel">Section</h3>
</div>
<div class="modal-body">
<%= f.input :section_name %>
</div>
<div class="modal-footer">
<button class="btn" data-dismiss="modal" aria-hidden="true">Close</button>
<button class="btn btn-primary">Save changes</button>
</div>
</div>
<div class="form-actions">
<%= f.button :submit, :class => 'btn-primary' %>
<%= link_to t('.cancel', :default => t("helpers.links.cancel")),
sections_path, :class => 'btn' %>
</div>
<% end %>
在Model Course_id部分中保存为nil。怎么解决这个?请帮我。
答案 0 :(得分:0)
@section = Course.find(course_id).sections.new