Unable to update property record data

Hi, I am having this weird error for the action controller where it says that there is a routing error and that the patch does not exist. I checked my routes and there was a patch path for the properties. I also added the patch for the properties in the routes but it still does not update the record instead refreshes the entire page and retrieves the previous data but does not update. It adds a new property record but does not update.

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 

def new
	@property = Property.new 

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

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

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
		render :edit
		puts 'The property update failed '  
		puts @property.inspect  
		puts @property.amenity.inspect

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


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])


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 Rails Routing from the Outside In — Ruby on Rails Guides

Defines the root path route (“/”)

root “articles#index”


List of Properties will come here

<% @properties.each do |property| %> <% end %>
Title Owner
<%= property.title %> <%= property.owner %> <%= link_to 'Edit', edit_admin_property_path(property) %>

<%= 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 %>

Try removing , url: admin_properties_path from your form_for, see if that helps.


Right now it looks like you’re submitting your update form to the same place where you should submit your create form, but urls are supposed to be different. For create, the url is admin_properties_path. For update, the url is admin_property_path(@property). However, if you don’t supply the url argument, the form_for should set it automatically.

Thanks Max, I updated the form url to

<%= form_for [:admin, @property], url: admin_property_path(@property), local: true, method: :patch do |f| %>

That did the trick and now the form is updating the data

Yeah, but now it will no longer create new data.

I created a separate if condition for that

<% if @property.new_record? %>

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

It creates new data as well

However, if you simply remove url and method, the form will do this automatically.

1 Like

I will give that a shot. Can I have your LinkedIn?

Sure, it’s listed at the top of my site.

1 Like