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
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 Rails Routing from the Outside In — Ruby on Rails Guides
Defines the root path route (“/”)
root “articles#index”
end
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 %>