If I am getting the question right, you want to create a has_and_belongs_to_many relationship between company and contact.
so in your Company.rb add
has_and_belongs_to_many :contacts
and in your Contact.rb add
has_and_belongs_to_many :companies
Now for this relationship create a new table companies_contacts with two fields 'company_id' and 'contact_id'
In company controller inside show action
@contact = Contact.new
In show page of company add this:-
<%= form_for @contact,:url => contacts_path(:company_id=> @company.id) do |f|%>
<%=f.label :name%>
<%=f.text_field :name%>
<%=f.button :submit%>
<%end%>
Now in the contact controller create action do like this:-
@contact = Contact.find_or_create_by_name(params[:contact][:name])
@contact.companies= Company.where(:id => params[:company_id])
@contact.save