Home > Blockchain >  Trouble with Ruby on Rails routing
Trouble with Ruby on Rails routing

Time:06-10

I have a page of dates and inside each date is the ability to add/delete employees.

I have a button on the employee's page that is supposed to send an email containing the employees that are currently on that page, which belong to that staff_date (just a date and an ID).

Here is my controller and button which is currently giving me this error.

 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"):

And the console error:

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)

Here is the routes:

resources :staff_dates do
  resources :employees, except: [:index, :show]
  post "send_email", on: :member
end

Here is the controller:

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

Here is the button:

<button href="/send_email" data-remote="true" 
data-method="post" >
Send Staffing</button>

CodePudding user response:

The problem is in your button url. You don't have url POST /send_email

You have POST /staff_dates/:staff_date_id/send_email

So the url button should be:

<%= button_to 'Send Staffing', send_email_staff_date_path(HERE_YOU_HAVE_TO_PROVIDE_STAFF_DATE_ID), method: :post, class: 'btn btn-secondary' %>

Replace HERE_YOU_HAVE_TO_PROVIDE_STAFF_DATE_ID with your staff_date_id

  • Related