我有一頁日期,每個日期內都可以添加/洗掉員工。
我在員工頁面上有一個按鈕,該按鈕應該發送一封電子郵件,其中包含當前在該頁面上的員工,這些員工屬于該員工日期(只是一個日期和一個 ID)。
這是我的控制器和按鈕,目前正在給我這個錯誤。
ActiveRecord::RecordNotFound (Couldn't find StaffDate with 'id'=undefined [WHERE "staff_dates"."user_id" = $1]):
09:12:08 web.1 |
09:12:08 web.1 | app/controllers/staff_dates_controller.rb:63:in `set_staff_date'
ActionController::RoutingError (No route matches [POST] "/staff_dates/1"):
和控制臺錯誤:
application-413731a5c4525b3d219e7962f4888785115e192f87cc41fe5c0816d2199eed1e.js:8810
Uncaught TypeError: Cannot destructure property 'element' of 'event.detail' as it is null.
at HTMLDocument.startFetchRequest (application-413731a5c4525b3d219e7962f4888785115e192f87cc41fe5c0816d2199eed1e.js:8810:13)
at Rails.fire (application-413731a5c4525b3d219e7962f4888785115e192f87cc41fe5c0816d2199eed1e.js:1944:19)
at Rails.handleRemote (application-413731a5c4525b3d219e7962f4888785115e192f87cc41fe5c0816d2199eed1e.js:2316:20)
at HTMLDocument.<anonymous> (application-413731a5c4525b3d219e7962f4888785115e192f87cc41fe5c0816d2199eed1e.js:1960:58)
以下是路線:
resources :staff_dates do
resources :employees, except: [:index, :show]
post "send_email", on: :member
end
這是控制器:
class EmployeesController < ApplicationController
before_action :set_staff_date
before_action :set_employee, only: %i[ show edit update destroy ]
def send_email
StaffMailer.with(employees: @staff_date.employees).send_staff.deliver_later
flash.now[:notice] = "Staff was successfully sent"
head :ok
end
def new
@employee = @staff_date.employees.build
end
def create
@employee = @staff_date.employees.build(employee_params)
if @employee.save
respond_to do |format|
format.html { redirect_to staff_date(@staff_date), notice: "Item was successfully created." }
format.turbo_stream { flash.now[:notice] = "Item was successfully created." }
end
else
render :new, status: :unprocessable_entity
end
end
def edit
end
def update
if @employee.update(employee_params)
respond_to do |format|
format.html { redirect_to line_staff_date_path(@employee), notice: "Item was successfully updated." }
format.turbo_stream { flash.now[:notice] = "Item was successfully updated." }
end
else
render :edit, status: :unprocessable_entity
end
end
def destroy
@employee.destroy
respond_to do |format|
format.html { redirect_to staff_date_path(@staff_date), notice: "Date was successfully destroyed." }
format.turbo_stream { flash.now[:notice] = "Date was successfully destroyed." }
end
end
private
def set_employee
@employee = @staff_date.employees.find(params[:id])
end
def employee_params
params.require(:employee).permit(:name, :employee_number, :comment)
end
def set_staff_date
@staff_date = current_user.staff_dates.find(params[:staff_date_id])
end
end
這是按鈕:
<button href="/send_email" data-remote="true"
data-method="post" class="btn btn-secondary">
Send Staffing</button>
uj5u.com熱心網友回復:
問題出在您的按鈕網址中。你沒有網址POST /send_email
你有POST /staff_dates/:staff_date_id/send_email
所以 url 按鈕應該是:
<%= button_to 'Send Staffing', send_email_staff_date_path(HERE_YOU_HAVE_TO_PROVIDE_STAFF_DATE_ID), method: :post, class: 'btn btn-secondary' %>
替換HERE_YOU_HAVE_TO_PROVIDE_STAFF_DATE_ID為您的staff_date_id
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/489073.html
