how to delete a record and go to delete page in rails 7 after installing ujs?

114 Views Asked by At

I am using Rails 7 and also install ujs but whenever i click on delete button Get request call rather than destroy request and i go to view record page rather then delete employee page. This is the code of delete link:

<%= link_to "Delete", employee_path(employee), method: :delete, class: "btn btn-danger", data: { confirm: "Are You sure to delete this employee" } %>

and this is the code of delete action from controller file:

def destroy
    @employee = Employee.find(params[:id])
    if @employee.destroy
      redirect_to employees_path, notice: "Employee has been deleted"
    end
  end
1

There are 1 best solutions below

0
On BEST ANSWER

As per the comments, you've partially installed rails-ujs, but you're still missing the import of the rails-ujs JS module.

You could try pinning the @rails/ujs module into your config/importmap.rb file and restarting the server:

# config/importmap.rb
pin "@rails/ujs", to: "https://ga.jspm.io/npm:@rails/[email protected]/lib/assets/compiled/rails-ujs.js"

See rails/importmap-rails for further info on pin.