0

Using Ruby on Rails 4, I'm trying to loop through my student list, and insert their ID's into the attendances table via a checkbox.

I'm getting the following error:

No route matches {:action=>"mark_attendance", :controller=>"attendance"} missing required keys: [:student_id, :attendance_id]

My files:

routes.rb

  resources :static
  resources :student do 
    resources :attendance
  end

  devise_for :users
  root :to => "static#index"

student_controller.rb

class StudentController < ApplicationController
    def index 
        @students = Student.all
    end

    def show 
        @student = Student.find(params[:id])
    end

    def create
        @student = Student.new(student_params)
        respond_to do |format|
            if @student.save
                format.html { redirect_to @student, notice: 'Student was successfully added.' }
                format.js
                format.json { render action: 'show', status: :created, location: @student }
            else
                format.html { render action: 'new' }
                format.json { render json: @student.errors, status: :unprocessable_entity }
            end
        end
    end

    def student_params
        params.require(:event).permit(:name, :contact, :balance)
    end 
end

student.rb

class Student < ActiveRecord::Base
  has_and_belongs_to_many :attendances
  attr_accessible :name, :contact, :balance
end

attendance_controller.rb

class AttendanceController < ApplicationController
    def create
        @student = Student.new(attendee_params)
        respond_to do |format|
            if @student.save
                format.html { redirect_to @student, notice: 'Students were added successfully.' }
                format.js
                format.json { render action: 'show', status: :created, location: @student }
            else
                format.html { render action: 'new' }
                format.json { render json: @student.errors, status: :unprocessable_entity }
            end
        end
    end

    def mark_attendance

    end

    def attendee_params
        params.require(:event).permit(:student_id, :date)
    end 
end

attendance.rb

class Attendance < ActiveRecord::Base
  belongs_to :student
  attr_accessible :student_id, :date
end

index.html.erb

<div class="row">
    <div class="content marketing">
        <div class="col-md-3"></div>
        <div class="col-md-6">
            <div class="table-responsive">
                <table class="table table-bordered table-hover">
                    <tr>
                        <th>&nbsp;</th>
                        <th>ID</th>
                        <th>Name</th>
                        <th>Contact</th>
                        <th>Balance</th>
                        <th style="width: 8%">&nbsp;</th>
                    </tr>
                    <% form_tag student_attendance_mark_attendance_path do %>
                        <% @students.each do |s| %>
                        <tr>
                            <td><%= check_box_tag "student_ids[]", s.id %></td>
                            <td><%= s.id %></td>
                            <td><%= s.name %></td>
                            <td><%= s.contact %></td>
                            <td><%= number_to_currency(s.balance) %></td>
                            <td><%= link_to "View", student_path(s.id), :class => "btn btn-primary" %></td>
                        </tr>
                        <% end %>
                    <% end %>
                </table>
            </div><!-- End Table Responsive -->
        </div><!-- End col-md-8 -->
        <div class="col-md-3"></div>
    </div>
</div><!-- End Row -->

I would love to know what the error is or how I would be able to do this using checkboxes.

1 Answer 1

1

You're using a path helper that needs parameters passed to it:

<% form_tag student_attendance_mark_attendance_path do %>

It apparently needs a Student, and Attendance. If you can't supply that, then you have a design problem.

Sign up to request clarification or add additional context in comments.

3 Comments

Should be: <%= form_tag...?
Yeah that'll be a problem too. :)
It looks like this seems to have solved it: As per vinodadhikary's answer, I changed it to: <%= form_tag mark_attendance_attendance_index_path do %> I also changed the routes file to: resources :attendance do post 'mark_attendance', on: :collection end ` Thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.