You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Ruby on Rails路由错误、StaffDate查询失败及JS异常问题求助

Troubleshooting Your Rails Staff Email Feature

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 no send_email method there
  • Your set_staff_date in EmployeesController uses params[:staff_date_id], but member routes for staff_dates pass params[: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 href attribute—use Rails route helpers to generate the correct path instead
  • The URL /send_email doesn'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_staff correctly handles the employees parameter passed via with
  • Confirm current_user is properly authenticated (your set_staff_date uses current_user.staff_dates.find, which is a good authorization guard)

内容的提问来源于stack exchange,提问作者Nicky Reds

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.28 22:57:37