Reputation: 387
I am showing a view where I show several records and I have a checkbox that I would like that when I change its value is sent via ajax the request to update the record, so far it works fine and updates, the problem is that when doing the respond_to still keeps the PATCH or PUT method to do the redirect. This is my checkbox:
<div class="form-check form-switch estado float-end">
<%= check_box_tag("reminder_status", value = "1", checked = @program.reminder.status, options = {id: "#{@program.reminder.id}", data_reminder_id: @program.reminder.id, class: 'form-check-input small toggle mt-2 mx-2 text-success', onchange: "reminder_status(this);"})%>
<%= label_tag "reminder_status", "#{t(:active)}", class: "form-control-placeholder pt-1 text-dark", id: "label_status" %>
</div>
<script type="text/javascript">
function reminder_status(chk) {
//function to update the completed field and mark the task as completed
$.ajax({
type: "PATCH",
url: "/programs/<%=@program.code%>/reminders/"+$(chk).attr('id'),
dataType: "script",
data: { reminder: {title : chk.checked} }
});
}
</script>
and this is the update method of reminders_controller:
def update
respond_to do |format|
if @reminder.update(reminder_params)
format.html { redirect_to show_program_client_path(@program.entity, program_id: @program.id, remove_modal: true), notice: "Reminders was successfully updated." }
else
format.html { render :edit, status: :unprocessable_entity }
format.json { render json: @reminder.errors, status: :unprocessable_entity }
end
end
end
and this is the result I get when I make some change in the check, first it is updating the reminder and it works fine but then when I do the respond_to it still keeps the PATCH method instead of GET for program when I just try to redirect.
Started PATCH "/programs/6xGUJq/reminders/161" for 127.0.0.1 at 2023-01-15 12:38:38 -0400
Processing by RemindersController#update as JS
Parameters: {"reminder"=>{"title"=>"false"}, "program_id"=>"6xGUJq", "id"=>"161"}
Program Load (0.4ms) SELECT "programs".* FROM "programs" WHERE "programs"."deleted_at" IS NULL AND "programs"."id" = $1 LIMIT $2 [["id", 175], ["LIMIT", 1]]
↳ app/controllers/reminders_controller.rb:107:in `set_program'
Reminder Load (0.3ms) SELECT "reminders".* FROM "reminders" WHERE "reminders"."deleted_at" IS NULL AND "reminders"."program_id" = $1 LIMIT $2 [["program_id", 175], ["LIMIT", 1]]
↳ app/controllers/reminders_controller.rb:103:in `set_reminder'
(0.1ms) BEGIN
↳ app/controllers/reminders_controller.rb:28:in `block in update'
Reminder Update (0.3ms) UPDATE "reminders" SET "title" = $1, "updated_at" = $2 WHERE "reminders"."id" = $3 [["title", "false"], ["updated_at", "2023-01-15 16:38:38.067467"], ["id", 161]]
↳ app/controllers/reminders_controller.rb:28:in `block in update'
(25.4ms) COMMIT
↳ app/controllers/reminders_controller.rb:28:in `block in update'
Client Load (0.4ms) SELECT "clients".* FROM "clients" WHERE "clients"."deleted_at" IS NULL AND "clients"."id" = $1 LIMIT $2 [["id", 6], ["LIMIT", 1]]
↳ app/controllers/reminders_controller.rb:30:in `block (2 levels) in update'
Redirected to http://localhost:3000/clients/LM9UBL/show_program?program_id=175&remove_modal=true
Completed 302 Found in 35ms (ActiveRecord: 27.0ms | Allocations: 7281)
Started PATCH "/clients/LM9UBL/show_program?program_id=175&remove_modal=true" for 127.0.0.1 at 2023-01-15 12:38:38 -0400
ActionController::RoutingError (No route matches [PATCH] "/clients/LM9UBL/show_program"):
actionpack (6.0.4.8) lib/action_dispatch/middleware/debug_exceptions.rb:36:in `call'
web-console (4.2.0) lib/web_console/middleware.rb:132:in `call_app'
web-console (4.2.0) lib/web_console/middleware.rb:28:in `block in call'
web-console (4.2.0) lib/web_console/middleware.rb:17:in `catch'
web-console (4.2.0) lib/web_console/middleware.rb:17:in `call'
actionpack (6.0.4.8) lib/action_dispatch/middleware/show_exceptions.rb:33:in `call'
railties (6.0.4.8) lib/rails/rack/logger.rb:37:in `call_app'
railties (6.0.4.8) lib/rails/rack/logger.rb:26:in `block in call'
activesupport (6.0.4.8) lib/active_support/tagged_logging.rb:80:in `block in tagged'
activesupport (6.0.4.8) lib/active_support/tagged_logging.rb:28:in `tagged'
activesupport (6.0.4.8) lib/active_support/tagged_logging.rb:80:in `tagged'
railties (6.0.4.8) lib/rails/rack/logger.rb:26:in `call'
sprockets-rails (3.4.2) lib/sprockets/rails/quiet_assets.rb:13:in `call'
actionpack (6.0.4.8) lib/action_dispatch/middleware/remote_ip.rb:81:in `call'
actionpack (6.0.4.8) lib/action_dispatch/middleware/request_id.rb:27:in `call'
rack (2.2.3) lib/rack/method_override.rb:24:in `call'
rack (2.2.3) lib/rack/runtime.rb:22:in `call'
activesupport (6.0.4.8) lib/active_support/cache/strategy/local_cache_middleware.rb:29:in `call'
actionpack (6.0.4.8) lib/action_dispatch/middleware/executor.rb:14:in `call'
actionpack (6.0.4.8) lib/action_dispatch/middleware/static.rb:126:in `call'
rack (2.2.3) lib/rack/sendfile.rb:110:in `call'
actionpack (6.0.4.8) lib/action_dispatch/middleware/host_authorization.rb:103:in `call'
webpacker (4.3.0) lib/webpacker/dev_server_proxy.rb:23:in `perform_request'
rack-proxy (0.7.2) lib/rack/proxy.rb:67:in `call'
railties (6.0.4.8) lib/rails/engine.rb:527:in `call'
puma (4.3.12) lib/puma/configuration.rb:228:in `call'
puma (4.3.12) lib/puma/server.rb:727:in `handle_request'
puma (4.3.12) lib/puma/server.rb:476:in `process_client'
puma (4.3.12) lib/puma/server.rb:332:in `block in run'
puma (4.3.12) lib/puma/thread_pool.rb:134:in `block in spawn_thread'
Upvotes: 0
Views: 38