# Passing data from action of cntrlr 1 to action of cntrlr 2

**URL:** <https://www.ruby-forum.com/t/passing-data-from-action-of-cntrlr-1-to-action-of-cntrlr-2/157150>\
**Category:** Rails\
**Created:** [January 7, 2009, 10:01pm UTC](https://www.ruby-forum.com/t/passing-data-from-action-of-cntrlr-1-to-action-of-cntrlr-2/157150 "2009-01-07T22:01:04Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![bhanub](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/bb73d2/32.png) [@bhanub](https://www.ruby-forum.com/u/bhanub)\
**Post date:** [January 7, 2009, 10:01pm UTC](https://www.ruby-forum.com/t/passing-data-from-action-of-cntrlr-1-to-action-of-cntrlr-2/157150/1 "2009-01-07T22:01:04Z")

</div>

The question is about passing data from action of one controller to  
action of another controller.

Inside the new\_patient\_controller i have the following code when the  
button “next” is clicked

```
def create_visit (created_patient)

```

redirect\_to(:controller=\>“patient\_visit”,:action=\>“index”,:created\_patient=\>created\_patient)  
end

This is suppose to call the index view of the “patient\_visit” controller  
.

Now the “patient\_visit” controller has the following form

1. \<% form\_for :patient\_visit , :url =\> { :action =\>  
:save\_patient\_visit } do |form| %\>
2. 
3. 

```
<p>

```

4. 

```
  <%= label :patient_visit , :complaints , "Complaints:" %>

```

5. 

```
  <%= form.text_area :complaints , :size => 20 %>

```

6. 

```
</p>

```

7. 
8. 

```
<p>

```

9. 

```
  <%= label :patient_visit , :systemic_history , "Systemic

```

History:" %\>  
10. \<%= form.text\_field :systemic\_history , :size =\> 40 %\>  
11.

  
12.  
13. 

14. \<%= label :patient\_visit , :opthal\_history , “Opthal  
History:” %\>  
15. \<%= form.text\_field :opthal\_history , :size =\> 40 %\>  
16.

  
17.  
18. \<%= submit\_tag “next” , :class =\> “submit” %\>  
19.  
20. \<%end%\>

Because of this the params which originally had the value of  
“created\_patient” now gets overwritten with the new params which has the  
value of “:patient\_visit” .

I need the created\_patient value , cause i need to attach the  
patient\_visit database entry to the created\_patient. ( since one patient  
has many visits )

---

<div class="post-metadata">

**Author:** ![bhanub](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/bb73d2/32.png) [@bhanub](https://www.ruby-forum.com/u/bhanub)\
**Post date:** [January 8, 2009, 10:30am UTC](https://www.ruby-forum.com/t/passing-data-from-action-of-cntrlr-1-to-action-of-cntrlr-2/157150/2 "2009-01-08T10:30:34Z")

</div>

Use the flash object to transfer data between controller actions.

Regards,  
Mukund

On Jan 8, 2:01 am, Lance K. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![bhanub](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/bb73d2/32.png) [@bhanub](https://www.ruby-forum.com/u/bhanub)\
**Post date:** [January 8, 2009, 10:52am UTC](https://www.ruby-forum.com/t/passing-data-from-action-of-cntrlr-1-to-action-of-cntrlr-2/157150/3 "2009-01-08T10:52:26Z")

</div>

On 8 Jan., 10:29, Mukund [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Use the flash object to transfer data between controller actions.

… or just add

, :created\_patient =\> params[:created\_patient]

to your form (where you specify the action that is called) so your  
param ain’t lost.

---

<div class="post-metadata">

**Author:** ![bhanub](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/bb73d2/32.png) [@bhanub](https://www.ruby-forum.com/u/bhanub)\
**Post date:** [January 8, 2009, 2:33pm UTC](https://www.ruby-forum.com/t/passing-data-from-action-of-cntrlr-1-to-action-of-cntrlr-2/157150/4 "2009-01-08T14:33:04Z")

</div>

Try something like:

def create\_visit(for\_patient)  
# This populates patient\_visit.patient\_id w/the proper id value  
patient\_visit = for\_patient.visits.new  
# Note that I’ve changed the action from index to new–that’s  
# the convention for actions that create new objects.  
redirect\_to(:controller=\>“patient\_visit”, :action=\>“new”)  
end

And then in your view, you add:

\<%= form.hidden\_field(:patient\_id) %\>

Anyplace inside your form\_for block.

That should get you a params[:patient\_visit] from which you can create a  
visit for a specific patient.

HTH,

-Roy

---

<div class="post-metadata">

**Author:** ![bhanub](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/bb73d2/32.png) [@bhanub](https://www.ruby-forum.com/u/bhanub)\
**Post date:** [January 8, 2009, 11:16am UTC](https://www.ruby-forum.com/t/passing-data-from-action-of-cntrlr-1-to-action-of-cntrlr-2/157150/5 "2009-01-08T11:16:00Z")

</div>

you can transmit values between controllers in the following ways:

- parameters to request (if not too long and not compromising  
security)
- session (recommended to store id, not objects)
