rails 中的删除按钮会销毁但不会重定向
Delete button destroys but not redirecting in rails
我有一个删除按钮,可以删除项目但不会重定向。我正在编辑视图中删除,所以我不确定这是否是问题所在。我检查了一下,它设置为删除而不是获取。
这是 Ruby on Rails 使用 HAML 的应用程序。
路线:
projects GET /projects(.:format) projects#index
POST /projects(.:format) projects#create
new_project GET /projects/new(.:format) projects#new
edit_project GET /projects/:id/edit(.:format) projects#edit
project PATCH /projects/:id(.:format) projects#update
PUT /projects/:id(.:format) projects#update
DELETE /projects/:id(.:format) projects#destroy
哈姆尔:
%div.actions-group-delete
.right
- if can? :destroy, @project
= link_to project_path(@project), method: :delete, remote: true, data: { confirm: 'Are you sure you want to permanently delete this project?' }, class: "btn btn--primary btn--auto btn--short btn--delete", title: "Delete project" do
%i.icon.icon-trash
项目总监:
def destroy
@project_id = params[:id]
project = Project.accessible_by(current_ability).find_by!(id: @project_id)
authorize! :destroy, @project
if @project.destroy.update_attributes(id: @project_id)
flash[:success] = "The Project was successfully deleted."
redirect_to projects_path
else
flash[:error] = "There was an error trying to delete the Project, please try again later."
redirect_to edit_project_path(@project)
end
end
项目模型:
class Project < ActiveRecord::Base
belongs_to :user
has_many :project_items, -> { order("code ASC, name ASC") }, dependent: :destroy
has_many :project_workers, dependent: :destroy
has_many :workforces, through: :project_workers
has_many :worked_hours, through: :project_workers
has_many :project_equipments, dependent: :destroy
has_many :equipments, through: :project_equipments
has_many :equipment_hours, through: :project_equipments
has_many :collaborators, dependent: :destroy
has_many :used_items, dependent: :destroy
has_many :reports, dependent: :destroy
# has_many :items_used, dependent: :destroy, through: :project_items, source: :used_items
accepts_nested_attributes_for :project_items, allow_destroy: true
accepts_nested_attributes_for :project_workers, allow_destroy: true
accepts_nested_attributes_for :project_equipments, allow_destroy: true
accepts_nested_attributes_for :collaborators
您的 link_to
设置为 remote: true
。这意味着 link 是通过 ajax 调用提交的,因此重定向发生在该调用的上下文中。
您需要删除 remote: true
或创建一个 delete.js.erb
视图和 return 从您的 delete
操作重定向到的路径。在视图中,您可以将 window.location
设置为此新路径。
我有一个删除按钮,可以删除项目但不会重定向。我正在编辑视图中删除,所以我不确定这是否是问题所在。我检查了一下,它设置为删除而不是获取。
这是 Ruby on Rails 使用 HAML 的应用程序。
路线:
projects GET /projects(.:format) projects#index
POST /projects(.:format) projects#create
new_project GET /projects/new(.:format) projects#new
edit_project GET /projects/:id/edit(.:format) projects#edit
project PATCH /projects/:id(.:format) projects#update
PUT /projects/:id(.:format) projects#update
DELETE /projects/:id(.:format) projects#destroy
哈姆尔:
%div.actions-group-delete
.right
- if can? :destroy, @project
= link_to project_path(@project), method: :delete, remote: true, data: { confirm: 'Are you sure you want to permanently delete this project?' }, class: "btn btn--primary btn--auto btn--short btn--delete", title: "Delete project" do
%i.icon.icon-trash
项目总监:
def destroy
@project_id = params[:id]
project = Project.accessible_by(current_ability).find_by!(id: @project_id)
authorize! :destroy, @project
if @project.destroy.update_attributes(id: @project_id)
flash[:success] = "The Project was successfully deleted."
redirect_to projects_path
else
flash[:error] = "There was an error trying to delete the Project, please try again later."
redirect_to edit_project_path(@project)
end
end
项目模型:
class Project < ActiveRecord::Base
belongs_to :user
has_many :project_items, -> { order("code ASC, name ASC") }, dependent: :destroy
has_many :project_workers, dependent: :destroy
has_many :workforces, through: :project_workers
has_many :worked_hours, through: :project_workers
has_many :project_equipments, dependent: :destroy
has_many :equipments, through: :project_equipments
has_many :equipment_hours, through: :project_equipments
has_many :collaborators, dependent: :destroy
has_many :used_items, dependent: :destroy
has_many :reports, dependent: :destroy
# has_many :items_used, dependent: :destroy, through: :project_items, source: :used_items
accepts_nested_attributes_for :project_items, allow_destroy: true
accepts_nested_attributes_for :project_workers, allow_destroy: true
accepts_nested_attributes_for :project_equipments, allow_destroy: true
accepts_nested_attributes_for :collaborators
您的 link_to
设置为 remote: true
。这意味着 link 是通过 ajax 调用提交的,因此重定向发生在该调用的上下文中。
您需要删除 remote: true
或创建一个 delete.js.erb
视图和 return 从您的 delete
操作重定向到的路径。在视图中,您可以将 window.location
设置为此新路径。