简体   繁体   中英

Multiple form rows with rails

I have a web app that I am building which will allow users to add courses and assignments they are taking. I would like them to be able to add multiple assignments at once. To do this I have a form that I will be adding javascript to add more input fields.

    <%= form_tag(@assignment) do |f| %>
      <div class="hidden-form">
        <input type="hidden" name="course_id" value="<%= @course.id %>">
      </div>
      <div id="assignments">
        <div class="assignment-row">
            <div class="fields">
                <div class="col-sm-6 col-xs-12">
                    <div class="form-group">
                        <%= label_tag :name %>
                        <%= text_field_tag "assignments[][name]", "", class: "form-control" %>
                    </div>
                </div>
                <div class="col-sm-3 col-xs-6">
                   <div class="form-group">
                    <%= label_tag :weight %>
                    <%= text_field_tag "assignments[][weight]","", class: "form-control" %>
                   </div>
                </div>
                <div class="col-sm-3 col-xs-6">
                    <div class="form-group">
                        <%= label_tag :grade %>
                        <%= text_field_tag "assignments[][grade]","", class: "form-control" %>
                    </div>
                </div>
            </div>
        </div>
    </div>
    <div class="col-md-12" style='margin: 15px 0;'>
        <button class="btn btn-primary btn-full" id="add-assignment-form">
        Add Assignment <span class="fa fa-plus"></span>
        </button
    </div>
   </div>
   <div class="footer">
    <div class="col-sm-6">
    <a href="#" class="btn btn-full btn-default">Skip</a>
        </div>
    <br class="visible-xs">
    <div class="col-sm-6 text-right">
    <%= submit_tag("Save and Continue", class: "btn btn-full btn-success") %>
    </div>
  <% end %>

I then have my assignment controller try to loop through each form and save each entry.

def create
    @assignments = params[:assignments]
    @course = Course.find(params[:course_id])

    @assignments.each do |assignment| 
      @a = @course.assignments.new(assignment)
      if @a.save 
        respond_to do |format| 
          format.html { redirect_to root_path, alert: "Course was added!" }
        end
      end
    end

  end

I am having trouble with this because I keep getting an error:ForbiddenAttributesError

I really don't know how to fix this. I have tried sending it to the sanitizer in my Assignment controller but nothing has worked...

NOTE: I have not added the dynamic form javascript functionality yet. Just want to get entries working first...

You must permit the params... something like the following should work:

def create
  @course = Course.find(params[:course_id])

  assignments_params.each do |assignment| 
    @a = @course.assignments.new(assignment)
    if @a.save 
      respond_to do |format| 
        format.html { redirect_to root_path, alert: "Course was added!" }
      end
    end
  end
end


private

def assignments_params
  params.permit(assignments: [:name, :weight, :grade])
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