属性记录未更新,因为未发现路由错误修补程序,尽管它仍然存在

Property record not updating due to routing error patch not discovered although it still exists

提问人:Asim Khan 提问时间:9/4/2023 更新时间:9/4/2023 访问量:25

问:

我遇到了这个奇怪的动作控制器错误,它说存在路由错误并且补丁不存在。我检查了我的路线,并且有属性的补丁路径。我还为路由中的属性添加了补丁,但它仍然没有更新记录,而是刷新整个页面并检索以前的数据,但不更新。它会添加新的属性记录,但不会更新。

class Amenity < ApplicationRecord
    belongs_to :property, inverse_of: :amenity 
end


class Property < ApplicationRecord
    has_one :amenity, dependent: :destroy, inverse_of: :property
    accepts_nested_attributes_for :amenity
end


class Admin::PropertiesController < ApplicationController
    before_action :authenticate_admin!

    def index 
        @properties = Property.all 
    end 

    def new
        @property = Property.new 
        @property.build_amenity 
    end 

    def create
        @property = Property.new(property_params)
        if @property.save 
            redirect_to admin_properties_path, notice: 'Property created successfully'
        else
            render :new
        end 
    end 

    def edit 
        @property = Property.find(params[:id])
        
        puts 'Before editing the value of current property is '  
        puts @property.inspect
        puts @property.amenity.inspect 
    end 

    def update 
        @property = Property.find(params[:id])
        
        puts 'About to update the property values, the current values are '  
        puts @property.inspect
        puts @property.amenity.inspect

        if @property.update(property_params)
            redirect_to admin_property_path(@property), notice: 'Property updated successfully'
            
            puts 'The new property values are '  
            puts @property.inspect
            puts @property.amenity.inspect
        else
            render :edit
            
            puts 'The property update failed '  
            puts @property.inspect  
            puts @property.amenity.inspect
        end 
    end 

    def destroy 
        @property = Property.find(params[:id])
        @property.destroy 
        redirect_to admin_properties_path, notice: 'Property was successfully deleted'
    end 

    private 

    def property_params
        params.require(:property).permit(:title, :name, :owner, :email, :coverage, 
        amenity_attributes: [:id, :has_pool, :has_garage, :has_balcony, :has_roof, :has_terrace, :has_kitchen, :has_storage, :has_barbq, 
        :has_gym, :has_studio, :has_cinema])
    end 
end


Rails.application.routes.draw do
  devise_for :admin, controllers: {
      sessions: 'admin/sessions'
  }
  
  devise_for :users, controllers: {
      sessions: 'users/sessions'
  }
  
  namespace :admin do
    resources :properties 
    patch '/admin/properties/:id', to: 'admin/properties#update', as: 'update' 
  end 


  namespace :users do
    resources :properties 
  end

  root 'pages#home'
  
  get 'pages/users'
  get 'pages/admin'

  # Define your application routes per the DSL in https://guides.rubyonrails.org/routing.html

  # Defines the root path route ("/")
  # root "articles#index"
end

<h1> List of Properties will come here </h1>

<table>
  <thead>
    <tr>
      <th>Title</th>
      <th>Owner</th>
    </tr>
  </thead>
  <tbody>
    <% @properties.each do |property| %>
    <tr>
      <td><%= property.title %></td>
      <td>
        <%= property.owner %>
      </td>
      <td>
        <%= link_to 'Edit', edit_admin_property_path(property) %>
        </td>
    </tr>
    <% end %>
  </tbody>
</table>

<%= link_to 'Add New Property', new_admin_property_path %>



<%= form_for [:admin, @property], url: admin_properties_path, local: :true do |f| %>

    <h3>Property Details</h3>
    
    <%= f.hidden_field :id %>

    <%= f.label :title %>
    <%= f.text_field :title %>

    <%= f.label :name %>
    <%= f.text_field :name %>

    <%= f.label :owner %>
    <%= f.text_field :owner %> 

    <%= f.label :email %>
    <%= f.text_field :email %>

    <%= f.label :coverage %>
    <%= f.text_field :coverage %> 

    <%= f.fields_for :amenity do |amenity_form| %>
        <%= amenity_form.hidden_field :id %>

        <%= amenity_form.label :has_pool, 'Has Pool?' %>
        <%= amenity_form.check_box :has_pool %> 

        <%= amenity_form.label :has_garage, 'Has Garage?' %>
        <%= amenity_form.check_box :has_garage %> 

        <%= amenity_form.label :has_balcony, 'Has Balcony?' %>
        <%= amenity_form.check_box :has_balcony %> 

        <%= amenity_form.label :has_roof, 'Has Roof?' %>
        <%= amenity_form.check_box :has_roof %> 

        <%= amenity_form.label :has_terrace, 'Has Terrace?' %>
        <%= amenity_form.check_box :has_terrace %>
        
        <%= amenity_form.label :has_kitchen, 'Has Kitchen?' %>
        <%= amenity_form.check_box :has_kitchen %>
        
        <%= amenity_form.label :has_storage, 'Has Storage?' %>
        <%= amenity_form.check_box :has_storage %> 

        <%= amenity_form.label :has_barbq, 'Has Bar BQ?' %>
        <%= amenity_form.check_box :has_barbq %>
        
        <%= amenity_form.label :has_gym, 'Has Gym?' %>
        <%= amenity_form.check_box :has_gym %> 

        <%= amenity_form.label :has_studio, 'Has Studio?' %>
        <%= amenity_form.check_box :has_studio %>
        
        <%= amenity_form.label :has_cinema, 'Has Cinema?' %>
        <%= amenity_form.check_box :has_cinema %> 
    <% end %>

    <%= f.submit (property.new_record? ? 'Create Prpoerty' : 'Update Property') %>
<% end %>type here

我想更新属性记录,但它已刷新并显示旧数据

Ruby-on-Rails Ruby-on-Rails-4

评论

0赞 Asim Khan 9/5/2023
我将表单URL更新为<%= form_for [:admin, @property],url:admin_property_path(@property),local:true,方法::p atch do |f|%> 这成功了,现在表单正在更新数据记录

答: 暂无答案