Property控制器destroy方法失效,删除后跳转异常问题排查
问题分析与修复方案
核心问题诊断
- 路由重复定义:
routes.rb中两次声明resources :properties,后一次覆盖前一次路由规则,导致DELETE请求被解析为GET请求的show动作,点击删除按钮直接跳转到properties/[id]页面,未触发destroy方法。 - 缺失
set_property方法:控制器声明了before_action :set_property但未实现该私有方法,会抛出错误中断destroy执行。 - 重定向目标错误:当前
destroy方法删除后重定向到全局properties列表,不符合返回当前房东properties/list页面的需求。 - HTTP方法失效:若未引入Rails UJS/Turbo,
link_to的method: :delete会失效,浏览器发送GET请求而非DELETE请求。
分步修复
1. 修复路由定义
打开config/routes.rb,删除重复的resources :properties,合并路由规则:
Rails.application.routes.draw do resources :wishlists resources :lease_relationships resources :reviews # 保留唯一的properties资源定义,合并所有自定义动作 resources :properties do collection do get "search" get "filter" get "create" get "list" get "results" post "results" end member do get "details" end end resources :landlords do collection do get "inquiries" end end resources :students resources :homepage get "signup", to: "users#new" get "login", to: "sessions#new" post "login", to: "sessions#create" delete "logout", to: "sessions#destroy" get "logout", to: "sessions#destroy" resources :users, except: [:new] get 'static_pages/about' root 'homepage#index' get "/landlord/reviews", to: "landlords#review" get "/properties/list", to: "properties#list" get "inquiries", to: "landlords#inquiries" get "/reviews/landlord", to: "reviews#landlord" post "/landlord_delete_inquiry", to: "landlords#delete_inquiry", as: "delete_inquiry" end
2. 完善Properties控制器
打开app/controllers/properties_controller.rb,补充set_property方法,移除重复代码并修正重定向目标:
class PropertiesController < ApplicationController before_action :set_property, only: %i[show edit update destroy] def index @properties = Property.all end def results @properties = Property.where("rent_per_month < ? AND number_of_rooms < ? AND number_of_bathrooms < ?", params[:Max_Price], params[:Rooms], params[:Bathrooms]) end def show # 移除重复的@property = Property.find(params[:id]),由before_action处理 end def list @landlord = Landlord.find(params[:landlord_id]) @properties = @landlord.properties end def new @property = Property.new end def create @property = Property.new(property_params) respond_to do |format| if @property.save format.html { redirect_to @property, notice: 'Property was successfully created.' } format.json { render :show, status: :created, location: @property } else format.html { render :new } # 修正之前错误的render :create format.json { render json: @property.errors, status: :unprocessable_entity } end end end def edit # 移除重复的@property = Property.find(params[:id]) end def update # 移除重复的@property = Property.find(params[:id]) if @property.update(property_params) redirect_to @property else render 'edit' end end def destroy @property.destroy flash[:success] = "Property deleted" # 重定向到当前房东的properties列表页面 redirect_to properties_list_path(landlord_id: @property.landlord_id) end private def set_property @property = Property.find(params[:id]) end def property_params # 修正参数名与schema匹配(原distance改为time) params.permit(:address, :max_occupancy, :rent_per_month, :number_of_rooms, :number_of_bathrooms, :landlord_id, :is_undergrad_allowed, :is_graduate_allowed, :time_from_campus_walking, :time_from_campus_driving, :has_garage_parking, :has_onstreet_parking, :in_unit_laundry, :is_pet_allowed, :is_utilities_included, :is_furnished, :additional_info) end end
3. 确保HTTP方法支持
- Rails 7+:在
app/views/layouts/application.html.erb中确认包含<%= turbo_include_tags %> - 旧版Rails:在
app/assets/javascripts/application.js中添加//= require jquery_ujs
4. 优化视图删除链接(可选)
在列表页面添加确认提示,避免误删:
<span><%= link_to 'Destroy', property_path(property), method: :delete, class: 'btn btn-danger', data: { confirm: 'Are you sure?' } %></span>
验证步骤
- 重启Rails服务器
- 进入目标房东的
properties/list页面 - 点击删除按钮,确认后会删除对应Property并跳转回当前房东的列表页面,列表不再显示已删除项
内容的提问来源于stack exchange,提问作者John Lervandal
相关产品推荐
相关产品推荐

