Ruby on Rails路由错误、StaffDate查询失败及JS异常问题求助
Hey there, let's break down these issues step by step—they're mostly rooted in routing-controller mismatches and incorrect URL generation, which are easy fixes once we align everything properly.
1. Why You're Getting ActiveRecord::RecordNotFound & Routing Errors
First, let's look at your routes:
resources :staff_dates do resources :employees, except: [:index, :show] post "send_email", on: :member # This is a member route tied to StaffDates end
When you define send_email as a member route under staff_dates, Rails expects this action to live in StaffDatesController, not EmployeesController. Right now, you've placed the send_email method in EmployeesController, which causes two key problems:
- The request hits
StaffDatesController(since the route is linked to it), but there's nosend_emailmethod there - Your
set_staff_dateinEmployeesControllerusesparams[:staff_date_id], but member routes forstaff_datespassparams[:id]instead—hence the "id=undefined" error when the button sends a malformed URL.
Fix: Move send_email to StaffDatesController
Add this to app/controllers/staff_dates_controller.rb:
def send_email StaffMailer.with(employees: @staff_date.employees).send_staff.deliver_later flash.now[:notice] = "Staff was successfully sent" head :ok end
And confirm your existing set_staff_date in this controller (which your error log confirms exists) looks like this:
private def set_staff_date @staff_date = current_user.staff_dates.find(params[:id]) end
2. Fixing the Button's Broken URL
Your current button uses an incorrect URL that doesn't pass the staff_date ID:
<button href="/send_email" data-remote="true" data-method="post" class="btn btn-secondary"> Send Staffing</button>
Two issues here:
- Buttons don't use the
hrefattribute—use Rails route helpers to generate the correct path instead - The URL
/send_emaildoesn't match your nested route structure (it should be/staff_dates/:id/send_email)
Fix: Generate the Correct URL with Rails Helpers
Use either link_to (styled as a button) or a button tag with the proper data-url:
Option 1: Use link_to (Rails-standard approach)
<%= link_to "Send Staffing", staff_date_send_email_path(@staff_date), method: :post, class: "btn btn-secondary", data: { turbo: true } %>
(If you're using older Rails with UJS instead of Turbo, replace data: { turbo: true } with data: { remote: true })
Option 2: Use a Button Tag
<button data-url="<%= staff_date_send_email_path(@staff_date) %>" data-method="post" data-turbo="true" class="btn btn-secondary"> Send Staffing </button>
This ensures the request sends a valid staff_date ID to the server.
3. Fixing the Browser Console TypeError
That Cannot destructure property 'element' of 'event.detail' as it is null error is a side effect of the invalid URL request. Once you fix the button's URL to point to the correct route with a valid ID, this error will disappear automatically—Turbo/UJS will have a valid request to process without hitting null values.
Quick Final Checks
- Verify
StaffMailer#send_staffcorrectly handles theemployeesparameter passed viawith - Confirm
current_useris properly authenticated (yourset_staff_dateusescurrent_user.staff_dates.find, which is a good authorization guard)
内容的提问来源于stack exchange,提问作者Nicky Reds

