# Sharing data between views?

**URL:** <https://www.ruby-forum.com/t/sharing-data-between-views/116938>\
**Category:** Rails\
**Created:** [October 11, 2007, 1:26pm UTC](https://www.ruby-forum.com/t/sharing-data-between-views/116938 "2007-10-11T13:26:32Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![gavin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/439d5e/32.png) [@gavin](https://www.ruby-forum.com/u/gavin)\
**Post date:** [October 11, 2007, 1:26pm UTC](https://www.ruby-forum.com/t/sharing-data-between-views/116938/1 "2007-10-11T13:26:32Z")

</div>

I’m new to Rails and this is surely and super easy question. I have a  
show\_companies.rhtml that produces a list of ships for each company.  
I have an add\_ship link next to each company which goes through the  
controller to a new\_ship view. How do I pass the company\_id from the  
original to this new view so I can have the company\_id available when  
I create the ship?

---

<div class="post-metadata">

**Author:** ![gavin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/439d5e/32.png) [@gavin](https://www.ruby-forum.com/u/gavin)\
**Post date:** [October 11, 2007, 1:34pm UTC](https://www.ruby-forum.com/t/sharing-data-between-views/116938/2 "2007-10-11T13:34:10Z")

</div>

:id =\> @company

which is a quick way of saying :id =\> @company.id

---

<div class="post-metadata">

**Author:** ![gavin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/439d5e/32.png) [@gavin](https://www.ruby-forum.com/u/gavin)\
**Post date:** [October 11, 2007, 2:58pm UTC](https://www.ruby-forum.com/t/sharing-data-between-views/116938/3 "2007-10-11T14:58:09Z")

</div>

I’m doing this simple thing:

\<% form\_for :ferry do |f| %\>  
\<%= f.hidden\_field :company\_id, :value =\> @company.id %\>  
Ferry name:  
  
\<%= f.text\_field :name %\>  
\<%= submit\_tag %\>  
\<% end %\>

…but I get this exception:

Couldn’t find Company without an ID.

Parameters:  
{“commit”=\>“Save changes”,  
“authenticity\_token”=\>“5398f203e605b0cbbeefe5123a67c3c3c424de8b”,  
“ferry”=\>{“name”=\>“adf”,  
“company\_id”=\>“4”}}

I can see the ID right there in the parameters. Why won’t it save?

---

<div class="post-metadata">

**Author:** ![gavin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/439d5e/32.png) [@gavin](https://www.ruby-forum.com/u/gavin)\
**Post date:** [October 11, 2007, 3:12pm UTC](https://www.ruby-forum.com/t/sharing-data-between-views/116938/4 "2007-10-11T15:12:15Z")

</div>

Well, I got it working, but I don’t know why the change made any  
difference. On the controller, I changed

@company = Company.find(params[:company\_id]) to @company\_id =  
params[:company\_id]

and in the view

\<%= f.hidden\_field :company\_id, :value =\> @company.id %\>  
to … :value =\> @company\_id %\>

The resulting type and assignment appears exactly the same to me.  
Anyone know what the difference is?

Gavin

On Oct 11, 9:00 pm, Dhaval P. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![gavin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/439d5e/32.png) [@gavin](https://www.ruby-forum.com/u/gavin)\
**Post date:** [October 11, 2007, 3:12pm UTC](https://www.ruby-forum.com/t/sharing-data-between-views/116938/5 "2007-10-11T15:12:51Z")

</div>

On 10/11/07, Gavin [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> …but I get this exception:
> 
> Couldn’t find Company without an ID.
> 
> Parameters:  
> {“commit”=\>“Save changes”,  
> “authenticity\_token”=\>“5398f203e605b0cbbeefe5123a67c3c3c424de8b”,  
> “ferry”=\>{“name”=\>“adf”,  
> “company\_id”=\>“4”}}
> 
> I can see the ID right there in the parameters. Why won’t it save?

The error is coming from your controller code. I’m guessing (since you  
didn’t show the code) that you have something like:

@company = Company.find(params[:id])

Based on the way you’ve set up your form, you would have to access the  
parameter as:

@company = Company.find params[:ferry][:company\_id]

But before you just throw that line of code in, step back for a sec.  
You’re not really approaching this correctly. You need to get a basic  
Rails book and work your way through it. Rails is “opinionated”, so if  
you learn to work with it instead of against it your life will be  
easier.

---

<div class="post-metadata">

**Author:** ![gavin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/439d5e/32.png) [@gavin](https://www.ruby-forum.com/u/gavin)\
**Post date:** [October 11, 2007, 3:00pm UTC](https://www.ruby-forum.com/t/sharing-data-between-views/116938/6 "2007-10-11T15:00:36Z")

</div>

in the controller u need to define @project = Project.find(params[:id])  
and then call @project.save method to make it work i think this should  
work propely
