ruby-on-rails 如何在安装ujs后在rails 7中删除记录并转到删除页面?

iih3973s  于 2023-08-08  发布在  Ruby
关注(0)|答案(1)|浏览(107)

我正在使用Rails7,也安装了ujs,但每当我点击删除按钮Get request call而不是destroy request时,我都会去查看记录页面而不是删除员工页面。这是删除链接的代码:

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

字符串
这是从控制器文件中删除动作的代码:

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

ycl3bljg

ycl3bljg1#

根据注解,您已经部分安装了rails-ujs,但仍然缺少rails-ujs JS模块的导入。
你可以尝试 * 固定 *@rails/ujs模块到你的config/importmap.rb文件中,然后重新启动服务器:

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

字符串
有关pin的更多信息,请参见rails/importmap-rails

相关问题