0

我是 ruby​​ on rails 的新手。我有两个模型,一个是课程,另一个是部分。 课程模型 course.rb

class Course < ActiveRecord::Base
     attr_accessible :course_name, :school_id
     #associations
     has_many :sections
    end

部分.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。如何解决这个问题?请帮我。

4

1 回答 1

0
@section = Course.find(course_id).sections.new
于 2013-07-06T05:18:47.507 回答