0

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.

yerassyl
  • 2,958
  • 6
  • 42
  • 68
  • you mean you want to send object with params ?, Best way is send id of the object to controller and create object there – Thorin Mar 31 '16 at 11:06
  • Maybe you can achieve this by declaring a static variable with `@@` symbol in controller and try to update it in the view page. – Muhammad Ali Mar 31 '16 at 11:22

2 Answers2

1

No, I don't think you can send an object from a view page to your controller. You have to pass the id of an object and again you have to find that object using the id from params in your controller .

dp7
  • 6,651
  • 1
  • 18
  • 37
1

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

Glorfindel
  • 21,988
  • 13
  • 81
  • 109
SunnyMagadan
  • 1,819
  • 14
  • 12