Forum: Ruby on Rails newbie questions: two tables - one form

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
F639e0cdf2fa098b535d6ec766b2aee1?d=identicon&s=25 Szymon Nowak (g0nzo)
on 2006-03-02 22:26
NOTE: i've previously posted it by mistake in test forum :)


I've got 2 tables (and models) - one with a product and second with
images of this product. How to create a single form (and controller)
that will insert/edit data to the product and also add links to images
table and add proper product_id? I've added "belongs_to :product" to
image model and "has_many :images" to product model.
How to connect these models, so if the user deletes a product all images
with corresponding product_id are deleted also?

Second thing - how to make a form field that user can just click a
button and choose a file rather than enter its path?

And the last thing - is mysql bool (or tinyint(1)) equivalent to
checkbox? So if i have bool field in a table and make a checkbox for it
in the form will it work?

Sorry if it was asked many times before, but the search option is
disabled now. If there was very similar question (here or on a web page)
you could just post a link.

Thanks in advance
F639e0cdf2fa098b535d6ec766b2aee1?d=identicon&s=25 Szymon Nowak (g0nzo)
on 2006-03-02 22:27
Ok i've managed to make it more or less (rather less) work in really
short time, but i've got few new questions:

- previously i was saving image name to image_url column, now i decided
to provide option for uploading the files and instead of the name of the
image (string), the whole image is saved to image_url (object:file). How
to get only its name without the path?

BTW. for some images i get object:file and for some object:stringIO in
image_url. Why???

- now user can only add only one image. How to make it so the user can
add i.e. 4 images (should i add [] somewhere in <%= file_field %> to
pass the whole array of images?).

- would it be easy to make the form like that: user sees only one file
input, if he chooses the file, new input appears (with correct name if
it's necessary) and so on. If he submits the form, all images (except
the empty field) are saved.

- how to delete images (files), if the corresponding product is deleted?
Fields in image table are deleted, but files are not.
This topic is locked and can not be replied to.