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 =>

I'm doing this simple thing:

<% form_for :ferry do |f| %>
  <%= f.hidden_field :company_id, :value => %>
    Ferry name:<br />
    <%= f.text_field :name %>
    <%= submit_tag %>
<% end %>

...but I get this exception:

Couldn't find Company without an ID.

{"commit"=>"Save changes",

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 =

and in the view

  <%= f.hidden_field :company_id, :value => %>
to ... :value => @company_id %>

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


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