提问人:Afaq Khan 提问时间:7/25/2023 最后编辑:Sebastián PalmaAfaq Khan 更新时间:7/26/2023 访问量:61
安装 UJS 后,如何在 Rails 7 中删除记录并转到删除页面?
how to delete a record and go to delete page in rails 7 after installing ujs?
问:
我正在使用 Rails 7 并安装了 ujs,但是每当我单击删除按钮获取请求调用而不是销毁请求时,我都会转到查看记录页面而不是删除员工页面。这是删除链接的代码:
<%= 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
答:
1赞
Sebastián Palma
7/25/2023
#1
根据评论,您已经部分安装了 rails-ujs,但仍然缺少 rails-ujs JS 模块的导入。
您可以尝试将 @rails/ujs 模块固定到您的文件中并重新启动服务器:config/importmap.rb
# config/importmap.rb
pin "@rails/ujs", to: "https://ga.jspm.io/npm:@rails/[email protected]/lib/assets/compiled/rails-ujs.js"
有关 的更多信息,请参见 rails/importmap-rails
。pin
评论
config/importmap.rb
app/javascript/application.js
pin "@rails/ujs", to: "https://ga.jspm.io/npm:@rails/[email protected]/lib/assets/compiled/rails-ujs.js"