rails 4 create form for extra fields in association table

时间:2016-02-12 21:21:40

标签: ruby-on-rails

I am trying to create a simple form for a student to select list of courses and enter remarks for each of the course

  1. course is a lookup table with name field
  2. student with name field etc..
  3. association table with course id, student-id,remarks

these are the associations I have in student model <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="content"> <input type="hidden" value="ABC123" class="imgRef"> <div class="input-group"> <input type="text" class="form-control" id="Item1" placeholder="description" value="" name="Item1" required> <button class="btn btn-default btn-fill addField"> BUTTON </button> </div> </div>

I have to create a form using rails 4 simple form that lists out all courses as check boxes and text field next to each check-box to fill in remarks for that course. I am able to generate the checkboxes fine but not sure how to create textbox next to each course..below is what I have..


    has_many :course_associations
    has_many :courses,  :through=> :course_associations
    accepts_nested_attributes_for :course_associations
    accepts_nested_attributes_for :courses

1 个答案:

答案 0 :(得分:0)

如果我理解正确,我会将simple_fields_for添加到您need to create text box for extra field in course association这样的表单中。

<%= course.simple_fields_for :course_associations do |association|
  <%= association.input :remarks(or whatever you've called it) %>
<% end %>

确保更新您的强力参数,我相信上述内容应该能够实现您的目标。

如果它不起作用,我可能不理解你的表单是如何设置的,并且需要将整个事情看作simple_form_for