1

The situation: I have a form partial that I want to use in two separate pages. After submit I want to update another part of the page with the newly added items but those differ depending of which page I'm on. Hence I can't rely on one single response from the controller to satisfy both types of targets. One is a table (which is hell to update with turbo-frames), the other is just a list.

Question: How to do this in shortest way possible?

My current solution to this is to pass a hidden field into with a custom redirect_to value being set as a local variable on the form

page.html.haml:

= render 'prescriptions/form', redirect_value: request.original_fullpath

_form.html.haml:

= form_with model: @prescription, url:[@patient, @prescription] do |f|
      = hidden_field_tag :redirect_to, redirect_value

prescription_controller.rb


      if @prescription.save 
        respond_to do |format|
          redirect_to params[:redirect_to] || patient_prescriptions_url(@patient), notice: "Prescription was successfully created."

At least I found this to be a hack to let turbo-frames "run the show" but still just lazy refresh the whole page w/o building specific frames and logic to determine what the controller should return. I guess I'm totally missing some obvious thing here so please educate me!

JUlinder
  • 995
  • 1
  • 8
  • 19
  • 1
    like `redirect_back`? https://api.rubyonrails.org/classes/ActionController/Redirecting.html#method-i-redirect_back_or_to – Alex Feb 20 '23 at 11:54
  • @Alex oh geez.. yeah exactly! Do one actually just read the whole rails-api to know these things? Is it perhaps that straight forward? – JUlinder Feb 21 '23 at 01:29
  • 1
    pretty much. just start by looking up things you already know, like `redirect_to`, and see what else is around. also ruby makes it easy to see everything you're working with: [ancestors](https://rubyapi.org/3.2/o/module#method-i-ancestors), [instance_methods](https://rubyapi.org/3.2/o/module#method-i-instance_method), [methods](https://rubyapi.org/3.2/o/object#method-i-methods) etc. passing `redirect_to` value with a form is ok too, say, when you need to redirect 2 steps back. – Alex Feb 21 '23 at 06:19

0 Answers0