Dropdown box

i am new to ruby on rails in my project i have two tables documents and
and in dependents there is a column called name and i want to populate
this column data in dropdown list and after selecting the name in the
dropdown list i want to store it in documents table column called
dependents_name for this what should be the controller and view.


Basic idea:


<%= form_tag({:action => ‘update’, :id => @document}) %>

collection_select(:document, :dependent_id, Dependent.find(:all), :id,

<%= submit_tag ‘Commit’ %>

(The form triggers the ‘update’ method, storing your @document.id in
params[:id]. The value that is selected by the user in the drop-down
box will be stored in params[:document][:dependent_id])


def update
@document = Document.find(params[:id])
# this line will update each attribute of @document with the
corresponding attribute in params[:document]. In this case, it’s the
same as if you did: @document.dependent_id =
params[:document][:dependent_id] and then @document.save.

This example stores the dependent_id rather than the dependant_name,
but that’s what you should store. Then in your Document model you
should have:

belongs_to :dependents

and in your Dependent model

has_many :documents

Then you can view the name of the dependent for any document like this: