ruby-on-railsruby-on-rails-4form-for

Access form_for object in corresponding controller method


I have a form_for, and I want to access variable of it in corresponding submit controller method. I'll explain what I mean: I have list of objects rendered as follows:

<%= render medical_situations %>

I corresponding medical_situation.html.erb file I specify how each object looks like, where inside of each I have form_for as follows:

<%= form_for medical_situation, {url: :send_to_pool_medical_situations, method: :post, remote:true} do |f| %>

In corresponding controller method I want to access that particular medical_situation object. Is it possible. I know I could pass medical_situation_id to find appropriate object but I am interested can I do it without extra request and code. In my send_to_poo method I want to do update that object.


Solution

  • In corresponding controller method I want to access that particular medical_situation object.

    It's not possible in any way, because http is stateless. Each new request is handled independently from the previous ones. The application will remove all in-memory variables after response rendering and start new incoming request handling from scratch with new and fresh controller instance, so the medical_situations variable won't exist any more. The only way how to tell the application which object you want to display is to pass object id in request parameters. It will allow you to fetch this object from the database and display it from your controller.

    what does it mean when they say http is stateless

    https://stackoverflow.com/questions/13200152/why-say-that-http-is-a-stateless-protocol