ruby-on-railsruby-on-rails-3ruby-on-rails-7rails-ujs

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


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

Solution

  • 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/ujs@7.0.6/lib/assets/compiled/rails-ujs.js"
    

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