Sharing data between views?

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?

:id => @company

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

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?

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. [email protected]

On 10/11/07, Gavin [email protected] 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.

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