Ok. I’m new at this, but let me try and explain my problem. I hope some
generous soul will be kind enough to offer some guidance…
Let’s say I have a form that accepts new input from an applicant. I
want to collect the applicant name (and other details) and a list of
two references, (actually more, but I’m trying to keep it simple).
Here’s a form I’ve created for this, (it’s called index.rhtml):
<%= start_form_tag :action => ‘save_applicant’ %>
Applicant Info
Name: <%= text_field "applicant", "name", "size" => 20 %>
Address: <%= text_field "applicant", "address", "maxsize" => 20 %>
Reference Info
Ref Name 1: <%= text_field "ref1", "name", "size" => 20 %>
Ref Email 1: <%= text_field "ref1", "email", "maxsize" => 20 %>
<p>
Ref Name 2:
<%= text_field "ref2", "name", "size" => 20 %>
</p>
<p>
Ref Email 2:
<%= text_field "ref2", "email", "maxsize" => 20 %>
</p>
<input type="submit" value="Save">
<%= end_form_tag %>
Ok. I’ve got two models set up. An ‘applicant’ model and a ‘ref’ model.
The applicant ‘has_many’ refs and the ref ‘belongs_to’ the applicant.
I’ve got validation going on in both models…just
‘validates_presence_of’ for all fields.
The controller I’m submitting to is called apply_controller. It
contains the following method:
def save_applicant
@applicant = Applicant.new(params[:applicant])
if @applicant.save
@applicant.refs.create(params[:ref1])
@applicant.refs.create(params[:ref2])
#go to an appropriate view to say ‘yeah, it worked’
else
render_action “index” #redisplay the index page
end
end
This all works fine as far as getting the data into the database goes.
But it does not validate the second model, i.e. the references. I can
leave them blank and not get an validation message.
Anyone know of a way to get the reference model to validate the data.
Also, I’m almost positive that my methodology here is not the greatest.
If you have any suggestions on a slicker ruby-esque way to accomplish
this same thing, I’d love to hear it.
Thanks in advance to any takers!