Two Models one form

I have setup a basic conference registration form. Within that form I
have an optional piece of information that would only be filled out if
the registrant was a vendor. Here is my relationship:

Registration has_one :vendor
Vendor belongs_to :registration

I want to be able to send both the vendor information and registration
information with the same form, but the vendor information might not be
sent if the registrant wasn’t a vendor. I have my fields set up for the
vendor but I don’t think they are right:

<%= select(“vendor”, “donation”, [[‘Door Prize’, ‘doorprize’],
[‘Monetary donation for door prize’, ‘monetary’], [‘Sponsor food
eventâ??breakfast ($700)’, ‘breakfast’], [‘Sponsor food eventâ??snack
($300)’, ‘snack’], [‘Sponsor food eventâ??dinner ($3000)’, ‘dinner’],
[‘Sponsor entertainment at dinner ($650)’, ‘entertainment’] ],
{:include_blank => true})%>

<%= select(“vendor”, “monetary_donation”, [[’$100’, ‘100’], [’$200’,
‘200’], [’$300’, ‘300’], [’$400’, ‘400’], [’$500’, ‘500’], [’$600’,
‘600’] ], {:include_blank => true})%>

<%= text_field ‘vendor’, ‘other’ %>

<%= check_box “vendor”, “electricity” %>

I get the error “Vendor expected, got String.” Do I need to manually
insert the vendor data in the controller? Like this:

@reg = Registration.new(params[:registration])
@reg.vendor.donation = params[:vendor_donation]
@reg.vendor.monetary_donation = params[:vendor_monetary_donation]
@reg.vendor.other = params[:vendor_other]
@reg.vendor.electricity = params[:vendor_electricity]

Or is there an easier way?

I could be wrong but I think you need to initialize a vendor. i.e:

@reg = Registration.new(params[:registration])
@reg.vendor = Vendor.new
@reg.vendor.donation =
etc…

-Adam

Oh, and for your other question you can do:

@reg.vendor = Vendor.new(params[:vendor])

and so long as your params hash is filled, that’ll work fine.

Does this relationship assume I have a registration_id field in my
vendors table?

I do and I think that is right but I can’t remember.

Yep.

-Adam

Adam B. wrote:

Oh, and for your other question you can do:

@reg.vendor = Vendor.new(params[:vendor])

and so long as your params hash is filled, that’ll work fine.

Does this relationship assume I have a registration_id field in my
vendors table?

I do and I think that is right but I can’t remember.

Will this not allow me to do validation on both models? It isn’t
picking up the validation on one of them.

Seth

I do believe that is because I am dumb. This is probably what you should
do:

@reg = Registration.new(params[:registration])
vend = Vendor.new(params[:vendor])
if vendor.save
@reg.vendor = vend
if @reg.save

else

end
else
<same as the else clause of the if @reg.save I believe>
end

Validation actually happens on save.

-Adam

Adam B. wrote:

Does this relationship assume I have a registration_id field in my
vendors table?

I do and I think that is right but I can’t remember.

Yep.

-Adam

Will this not allow me to do validation on both models? It isn’t
picking up the validation on one of them.

Seth

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs