Rails 4 - 循环复选框和插入

时间:2013-12-21 07:35:06

标签: ruby-on-rails ruby checkbox

使用Ruby on Rails 4,我试图遍历我的学生列表,并通过复选框将他们的ID插入考勤表。

我收到以下错误:

  

没有路线匹配{:action =>“mark_attendance”,   :controller =>“出勤”}缺少必需的密钥:[:student_id,   :attendance_id]

我的档案:

的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 -->

我很想知道错误是什么,或者我怎么能使用复选框来执行此操作。

1 个答案:

答案 0 :(得分:1)

您正在使用需要传递给它的参数的路径助手:

<% form_tag student_attendance_mark_attendance_path do %>

显然需要学生和出勤。如果你不能提供,那你就有设计问题。