简体   繁体   中英

many-to-many with has_many :through association nested form

student.rb

class Student < ActiveRecord::Base
  has_many :enrollments
  has_many :courses, through: :enrollments

  accepts_nested_attributes_for :enrollments
end

enrollment.rb

class Enrollment < ActiveRecord::Base
  belongs_to :student
  belongs_to :course
end

course.rb

class Course < ActiveRecord::Base
  has_many :enrollments
  has_many :students, through: :enrollments
end

enrollments_controller.rb

class EnrollmentsController < ApplicationController

  def new
    @current_student = current_user.student
    @enrollments =  @current_student.enrollments.build
  end

  def create
    current_student = current_user.student
    @enrollments =  current_student.enrollments.build(enrollment_params)
      if @enrollments.save
          flash[:success] = "You have successfully enrolled."
          redirect_to new_enrollment_path
      else
          # fail
          flash[:danger] = "Please try again."
          redirect_to new_enrollment_path
      end
  end

  private
    def enrollment_params
        params.require(:enrollment).permit(:student_id, :course_id)
    end

end

enrollment/new.html.erb

<%= nested_form_for(@current_student, html: { class: 'form-horizontal' }) do |f| %>

  <%= f.fields_for :enrollments do |e| %>
    <div class="form-group">
      <%= e.label :course_id, for: :course_id, class: 'col-xs-3 control-label' %>
        <div class="col-xs-9">
          <%= e.collection_select :course_id, Course.all, :id, :name, {prompt: "Select your Course"}, {class: 'form-control'} %>
        </div>
    </div>
  <% end %>

  <%= f.link_to_add 'Add Course', :enrollments, class: "col-xs-9 col-xs-offset-3" %>

    <div class="form-group">
      <div class="col-xs-9 col-xs-offset-3">
        <%= f.submit "Enroll Now", class: 'btn btn-primary' %>
      </div>
    </div>

<% end %>

By referering to :

many-to-many: has_many :through association form with data assigned to linking model create form view

Intention: Create the Enrollments with the existing courses and students.

Current implementation of enrollment/new.html.erb will show all of the existing enrollments on the form which is not the desired presentation view.

I wish to create a blank form for creating enrollments. How can I do that?

By just adding a line, "!e.object.persisted?" it solves the problem.

enrollment/new.html.erb

<%= f.fields_for :enrollments do |e| %>

    <!--  -->
    <% if !e.object.persisted? %>
      <div class="form-group">
        <%= e.label :course_id, for: :course_id, class: 'col-xs-3 control-label' %>
          <div class="col-xs-9">
            <%= e.collection_select :course_id, Course.all, :id, :name, {prompt: "Select your Course"}, {class: 'form-control'} %>
          </div>
      </div>
    <% end %>
<% end %>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM