很抱歉,但我已经有40个小时的时间来解决这个问题,(我是新手)我已经回顾了与此错误相关的类似问题,但未能找到解决方案。
我有一个拥有员工的分支模型,那些员工有出勤,所以当我想保存员工的出勤时我得到了这个错误: https://s3.amazonaws.com/refaccionariasdelvalle/rails-error.png
错误代码
ArgumentError in AttendancesController#create
wrong number of arguments (given 0, expected 1)
Extracted source (around line #82):
80
81
82 def attendance_params(my_params)
83 my_params.permit(:present, :employee_id)
84 end
85
我的控制器:
class AttendancesController < ApplicationController
before_action :authenticate_user!
load_and_authorize_resource
before_action :set_and_authorize_branch, except: :index_employees
layout :user_profile_layout
def show
@attendances = Attendance.joins(:employee).where(date: params[:date], employee_id: @branch.employees.ids).order('first_name')
@date = Date.parse(params[:date])
end
def index_employees
@employee = Employee.find(params[:id])
@attendances = @employee.attendances
@events = []
@attendances.each do |attendance|
event = {}
event[:title] = attendance.present.to_s
event[:start] = attendance.date.to_s
event[:rendering] = "background"
attendance.present ? event[:backgroundColor] = "#9EB25D" :
event[:backgroundColor] = "#CC4B4B"
@events << event
end
end
def new
redirect_to attendance_branch_path(id: @branch, date: Date.today) if !Attendance.where(date: Date.today, employee_id: @branch.employees.ids).empty?
@attendances = []
@branch.employees.each do |employee|
@attendances << Attendance.new(employee_id: employee.id)
end
@attendances = @attendances.sort_by { |att| att.employee.first_name }
end
def create
params[:attendances].each do |attendance|
new_attendance = Attendance.new(attendance_params(attendance))
new_attendance.date = Date.today
new_attendance.save
end
redirect_to branch_class_path(@branch), notice: 'Attendance taken
successfully'
end
def edit
@attendances = Attendance.joins(:employee).where(date: params[:date],
employee_id: @branch.employees.ids).order('first_name')
@date = Date.parse(params[:date])
end
def update
params[:attendances].each do |id, params|
attendance = Attendance.find(id)
attendance.update(attendance_params(params))
end
redirect_to attendance_branch_path(id: @branch, date: params[:date])
end
private
def user_profile_layout
if user_signed_in? && current_user.role.name === "Branch"
"layout_for_user_branch"
#elsif user_signed_in? && current_user.role.name === "Admin"
else
"application_for_user_admin"
end
end
def set_and_authorize_branch
@branch = Branch.find(params[:id])
end
def attendance_params(my_params)
my_params.permit(:present, :employee_id)
end
end
我的模特:
class Attendance < ApplicationRecord
belongs_to :employee
validates :employee_id, uniqueness: { scope: :date }
end
出勤/ _form.html.erb
<%= form_tag path, form_options do %>
<h4>Swipe left for absent, right for present!</h4>
<div class="attendance">
<% @attendances.each_with_index do |attendance, index| %>
<%= fields_for 'attendances[]', attendance do |f| %>
<div class="attendance-card <%= set_background_color(attendance) %>" data-id="<%= attendance.employee.id %>">
<div class="employee-name">
<h4><%= attendance.employee.first_name %> <%= attendance.employee.last_name.first %>.</h4>
</div>
<div class="attendance-status">
<% if attendance.present %>
<div class="employee-present"><i class="fa fa-check color-green" aria-hidden="true"></i></div>
<% elsif attendance.present == false %>
<div class="employee-absent"><i class="fa fa-times color-red" aria-hidden="true"></i></div>
<% else %>
<% if index == 0 %>
<div class="swipe-instructions-left"><i class="fa fa-hand-o-left color-red floating-horizontal-left" aria-hidden="true"></i></i></div>
<div class="swipe-instructions-right"><i class="fa fa-hand-o-right color-green floating-horizontal-right" aria-hidden="true"></i></div>
<% end %>
<% end %>
</div>
<%= f.text_field :present, type: 'hidden'%>
<%= f.text_field :employee_id, type: 'hidden'%>
</div>
<% end %>
<% end %>
</div>
<div class="sticky-footer">
<input class='btn edu-button' type="submit" value="Submit" form="attendance">
</div>
<% end %>
<script>
var swipe_present = function() {
$('.attendance-card').hammer().bind("swiperight", function(event){
var id = $(this).attr('data-id');
$('[data-id=' + id + '] input').first().val('true');
var $employee = $('[data-id=' + id + '].attendance-card');
$employee.removeClass("attendance-red");
$employee.addClass("attendance-green");
$('[data-id=' + id + '] .attendance-status').html('<div class="employee-present"><i class="fa fa-check color-green" aria-hidden="true"></i></div>');
$employee.hide("slide", {direction: "right"}, 200, function(){
$employee.appendTo('.attendance').show();
});
});
};
var swipe_absent = function(){
$('.attendance-card').hammer().bind("swipeleft", function(event){
var id = $(this).attr('data-id');
$('[data-id=' + id + '] input').first().val('false');
var $employee = $('[data-id=' + id + '].attendance-card');
$employee.removeClass("attendance-green");
$employee.addClass("attendance-red");
$('[data-id=' + id + '] .attendance-status').html('<div class="employee-absent"><i class="fa fa-times color-red" aria-hidden="true"></i></div>');
$employee.hide("slide", {direction: "left"}, 200, function(){
$employee.appendTo('.attendance').show();
});
});
};
$(document).ready(function(){
swipe_present();
swipe_absent();
});
</script>
路由
resources :branches do
resources :employees, only: [ :index, :new, :create ]
member do
resources :attendances, only: [ :new, :create ]
get 'attendance/:date', to: 'attendances#show', as: 'attendance'
get 'attendance/:date/edit', to: 'attendances#edit', as: 'edit_attendance'
patch 'attendance/:date', to: 'attendances#update', as: 'update_attendance'
end
end
表格
create_table "attendances", force: :cascade do |t|
t.date "date"
t.boolean "present"
t.boolean "excused"
t.integer "employee_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.index ["employee_id", "created_at"], name: "index_attendances_on_employee_id_and_created_at", using: :btree
t.index ["employee_id"], name: "index_attendances_on_employee_id", using: :btree
end
我认为问题在于我将布尔:present 的值传递给数据库,但如果是这样,我还是不知道如何使其工作。
感谢您的帮助!
更新
首先,感谢@Sebastían,@ Gerry和@Pavan感兴趣帮助我。
现在,这是如何在控制台上运行而不是从应用程序运行?
2.3.0 :019 > Employee.where(branch_id: b).each do |e|
2.3.0 :020 > new_attendance = Attendance.new(employee_id: e.id, date: Date.today)
2.3.0 :021?> new_attendance.present = [true, false].sample
2.3.0 :022?> new_attendance.save
2.3.0 :023?> end
Employee Load (0.7ms) SELECT "employees".* FROM "employees" WHERE "employees"."branch_id" = 3
(0.2ms) BEGIN
Attendance Exists (0.5ms) SELECT 1 AS one FROM "attendances" WHERE "attendances"."employee_id" = $1 AND "attendances"."date" = $2 LIMIT $3 [["employee_id", 4], ["date", Sat, 08 Jul 2017], ["LIMIT", 1]]
(0.6ms) SELECT MAX("attendances"."id") FROM "attendances"
SQL (1.3ms) INSERT INTO "attendances" ("id", "date", "present", "employee_id", "created_at", "updated_at") VALUES ($1, $2, $3, $4, $5, $6) RETURNING "id" [["id", 35], ["date", Sat, 08 Jul 2017], ["present", true], ["employee_id", 4], ["created_at", 2017-07-08 18:51:01 UTC], ["updated_at", 2017-07-08 18:51:01 UTC]]
(5.8ms) COMMIT
(0.3ms) BEGIN
Attendance Exists (0.5ms) SELECT 1 AS one FROM "attendances" WHERE "attendances"."employee_id" = $1 AND "attendances"."date" = $2 LIMIT $3 [["employee_id", 2], ["date", Sat, 08 Jul 2017], ["LIMIT", 1]]
(0.5ms) SELECT MAX("attendances"."id") FROM "attendances"
SQL (0.6ms) INSERT INTO "attendances" ("id", "date", "present", "employee_id", "created_at", "updated_at") VALUES ($1, $2, $3, $4, $5, $6) RETURNING "id" [["id", 36], ["date", Sat, 08 Jul 2017], ["present", false], ["employee_id", 2], ["created_at", 2017-07-08 18:51:01 UTC], ["updated_at", 2017-07-08 18:51:01 UTC]]
(5.9ms) COMMIT
制作行动
def create
params[:attendances].each do |attendance|
new_attendance = Attendance.new(attendance_params(attendance))
new_attendance.date = Date.today
new_attendance.save
end
redirect_to attendance_branch_path(id: @branch, date: new_attendance.date), notice: 'Attendance taken successfully'
end
更新操作
def update
params[:attendances].each do |id, params|
attendance = Attendance.find(id)
attendance.update(attendance_params(params))
end
redirect_to attendance_branch_path(id: @branch, date: params[:date])
end
PARAMS
def attendance_params(my_params)
my_params.permit(:present, :employee_id)
end
在从控制台创建这些记录之后,我可以转到应用程序并对这些记录进行更新并且工作正常。 但是,如果我去控制台并删除之前创建的那两个最后记录,使其再次出现以标记应用程序中的出席我得到相同的错误:
ArgumentError in AttendancesController#create
wrong number of arguments (given 0, expected 1)
Extracted source (around line #81):
81: def attendance_params(my_params)
82: my_params.permit(:present, :employee_id)
83: end
我真的不明白
答案 0 :(得分:2)
strong_params
不接受争论。您可以根据需要标记一个哈希值,该哈希值将通过您的参数传递给您的控制器并在那里指定允许的密钥。
所以,相反,定义attendance_params
期望一个名为my_params
的参数,那么你可以把它变成一个“非参数”方法,而不是使用my_params
,然后使用params
实例公共方法,该方法将返回一个新的ActionController::Parameters
对象,该对象使用request.parameters
初始化。
您可以尝试:
def attendance_params
params.permit(:present, :employee_id)
end