【问题标题】:My Destroy Method in my Property Controller isn't working我的 Property Controller 中的 Destroy 方法不起作用
【发布时间】:2022-11-15 04:48:35
【问题描述】:

我有一个页面列出了特定房东拥有的所有属性,我正在尝试添加一个删除该属性的按钮,但它目前无法正常工作并重定向到错误的页面。单击销毁按钮后,我希望它重定向回属性/列表。

架构:

# This file is auto-generated from the current state of the database. Instead
# of editing this file, please use the migrations feature of Active Record to
# incrementally modify your database, and then regenerate this schema definition.
#
# This file is the source Rails uses to define your schema when running `bin/rails
# db:schema:load`. When creating a new database, `bin/rails db:schema:load` tends to
# be faster and is potentially less error prone than running all of your
# migrations from scratch. Old migrations may fail to apply correctly if those
# migrations use external dependencies or application code.
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema[7.0].define(version: 2022_11_10_101242) do
  # These are extensions that must be enabled in order to support this database
  enable_extension "plpgsql"

  create_table "landlords", force: :cascade do |t|
    t.string "name"
    t.string "email"
    t.string "review_overall"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
  end

  create_table "lease_relationships", force: :cascade do |t|
    t.bigint "student_id", null: false
    t.bigint "property_id", null: false
    t.bigint "landlord_id", null: false
    t.date "lease_start_date"
    t.date "lease_end_date"
    t.boolean "paid_security_deposit"
    t.boolean "paid_last_month_deposit"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
    t.index ["landlord_id"], name: "index_lease_relationships_on_landlord_id"
    t.index ["property_id"], name: "index_lease_relationships_on_property_id"
    t.index ["student_id"], name: "index_lease_relationships_on_student_id"
  end

  create_table "properties", force: :cascade do |t|
    t.boolean "is_graduate_allowed"
    t.integer "max_occupancy"
    t.string "address"
    t.boolean "is_undergrad_allowed"
    t.bigint "landlord_id", null: false
    t.integer "rent_per_month"
    t.integer "time_from_campus_walking"
    t.integer "time_from_campus_driving"
    t.boolean "has_onstreet_parking"
    t.boolean "has_garage_parking"
    t.integer "number_of_rooms"
    t.integer "number_of_bathrooms"
    t.boolean "in_unit_laundry"
    t.boolean "is_pet_allowed"
    t.boolean "is_utilities_included"
    t.boolean "is_furnished"
    t.text "additional_info"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
    t.index ["landlord_id"], name: "index_properties_on_landlord_id"
  end

  create_table "reviews", force: :cascade do |t|
    t.bigint "landlord_id", null: false
    t.bigint "student_id", null: false
    t.integer "score"
    t.text "review"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
    t.index ["landlord_id"], name: "index_reviews_on_landlord_id"
    t.index ["student_id"], name: "index_reviews_on_student_id"
  end

  create_table "students", force: :cascade do |t|
    t.string "name"
    t.string "email"
    t.string "class_standing"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
  end

  create_table "users", force: :cascade do |t|
    t.string "username"
    t.string "email"
    t.string "password_digest"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
  end

  create_table "wishlists", force: :cascade do |t|
    t.bigint "property_id", null: false
    t.bigint "student_id", null: false
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
    t.boolean "is_interested"
    t.index ["property_id"], name: "index_wishlists_on_property_id"
    t.index ["student_id"], name: "index_wishlists_on_student_id"
  end

  add_foreign_key "lease_relationships", "landlords"
  add_foreign_key "lease_relationships", "properties"
  add_foreign_key "lease_relationships", "students"
  add_foreign_key "properties", "landlords"
  add_foreign_key "reviews", "landlords"
  add_foreign_key "reviews", "students"
  add_foreign_key "wishlists", "properties"
  add_foreign_key "wishlists", "students"
end

属性控制器:

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])
    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 :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 = Property.find(params[:id])
        print("destroying")
        @property.destroy
        flash[:success] = "Property deleted"
        redirect_to properties_url
    end

    private 

    def property_params
        params.permit(:address, :max_occupancy, :rent_per_month, :number_of_rooms, :number_of_bathrooms, :landlord_id, :is_undergrad_allowed, :is_graduate_allowed, :distance_from_campus_walking, :distance_from_campus_driving, :has_garage_parking, :has_onstreet_parking, :in_unit_laundry, :is_pet_allowed, :is_utilities_included, :is_furnished, :additional_info)
    end

end

路线.rb:

Rails.application.routes.draw do
  resources :wishlists
  resources :lease_relationships
  resources :reviews

  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 :properties
  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

列表页:

 <%= link_to "Create New Property", "/properties/create" %>

<h1>Your Properties</h1>
<% @properties.each do |property| %>
    <div class = "lol" >
    <p class = "pagination">
    <h3>Address: <%= property.address %></h3>
    <h3>Max Occupancy: <%= property.max_occupancy %></h3>
    <h3>Monthly Rent: $<%= property.rent_per_month %></h3>
    <span><%= link_to 'Destroy', property_path(property), method: :delete, class: 'btn btn-danger' %></span>
    
    </div>
<% end %>
</div>

我尝试将 destroy 方法添加到控制器,一旦我按下列表视图上的按钮,它就会转到 properties/1 或它所在的任何 property_id,一旦你返回它就不会从列表中删除该属性并且它仍然出现。

【问题讨论】:

  • rails routes | grep properties的结果是什么?我有一种有趣的感觉,你的销毁路线链接是错误的
  • 删除 /properties/:id(.:format) 属性#destroy
  • @jax - 不。这是经典的“Rails UJS 不工作,所以链接正在做链接实际应该做的事情”的 Rails 7 版本。

标签: ruby-on-rails ruby


【解决方案1】:

使用 &lt;%= button_to 'Destroy', property_path(property), method: :delete, class: 'btn btn-danger' %&gt; 而不是 link_to。这创建了一个只包含一个按钮的表单,不需要任何 JS 技巧就可以工作,并且从可访问性的角度来看更好。

如果您出于某种原因仍想使用&lt;A&gt; 元素,则需要确保为Turbo 使用正确的data-turbo-method="DELETE" 属性,而不是Rails UJS 在以前版本中使用的data-method="DELETE" 属性。

<span><%= link_to 'Destroy', property_path(property), data: { turbo_method: :delete }, class: 'btn btn-danger' %></span>

如果它仍然不起作用,您需要确保 Turbo 包含在页面中并正常工作。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-10-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-06-23
    • 1970-01-01
    • 2012-11-18
    相关资源
    最近更新 更多