Forum: Ruby on Rails Rais 3, nested attributes problem with paperclip

Ded91d26e72879bcd3c2929a71ec1d9b?d=identicon&s=25 Johndel Deliyiannis (johndel)
on 2011-05-28 02:24
(Received via mailing list)
I have two models, one model girl and one girl_photos (a file upload
asset). The only code I have in models which describes the association
is:

For the girl => belongs_to :girl and  has_attached_file :photo
For the model girl_photos => has_many  :girl_photos and
accepts_nested_attributes_for :girl_photos

I've tried to save as a nested attribute the photo, it saves the
association in the database but it doesn't set the name of the image
and doesn't upload it.

I thought that it might be a problem with paperclip so I created
another test model with no association, just one image upload (in the
same model) and it worked fine (it uploaded the images and saved it
correctly).

In the log file I see the following:
INSERT INTO `girls` (`name`, `surname`, `height`, `weight`,
`description`,  `created_at`, `updated_at`) VALUES ('test', 'test',
'test', 'test', 'test', '2011-05-27 18:31:57', '2011-05-27 18:31:57')

INSERT INTO `girl_photos` (`created_at`, `updated_at`,
`photo_file_name`, `photo_content_type`, `photo_file_size`,
`photo_updated_at`, `girl_id`) VALUES ('2011-05-27 18:31:57',
'2011-05-27 18:31:57', NULL, NULL, NULL, NULL, 4)
[paperclip] Saving attachments.

For some reason, on the second query, it saves the photo but it
doesn't captures the name and it doesn't upload it. I don't know what
else could I try or test.

Thank you for your help.
6799ecb7745167d2590d85c4ed25d985?d=identicon&s=25 David Kahn (dkahn)
on 2011-05-28 02:45
(Received via mailing list)
On Fri, May 27, 2011 at 7:23 PM, Yiannis <istoselidas@gmail.com> wrote:

> I have two models, one model girl and one girl_photos (a file upload
> asset). The only code I have in models which describes the association
> is:
>
> For the girl => belongs_to :girl and  has_attached_file :photo
> For the model girl_photos => has_many  :girl_photos and
> accepts_nested_attributes_for :girl_photos
>

First, the above does not make sense... do you mean the following:

Model Girl
has_many :girl_photos

Model GirlPhotos
belongs_to :girl

I am assuming that your GirlPhoto model has a girl_id field, correct?

Anyhow confirm this as if the above is as you say, the associations look
wrong.
Ded91d26e72879bcd3c2929a71ec1d9b?d=identicon&s=25 Johndel Deliyiannis (johndel)
on 2011-05-28 03:03
(Received via mailing list)
Yes! Sorry David I meant exactly what you wrote and yes my GirlPhoto
model has a girl_id. Sorry for the wrong information.

I am also using paperclip 2.3.8 and rails 3.0.6. I can't figure out
what is wrong, I checked on google and I found a tutorial which is
exactly what I have done (the tutorial =>
http://sleekd.com/general/adding-multiple-images-t...).
6799ecb7745167d2590d85c4ed25d985?d=identicon&s=25 David Kahn (dkahn)
on 2011-05-28 03:07
(Received via mailing list)
2011/5/27 Yiannis <istoselidas@gmail.com>

> Yes! Sorry David I meant exactly what you wrote and yes my GirlPhoto
> model has a girl_id. Sorry for the wrong information.
>
> I am also using paperclip 2.3.8 and rails 3.0.6. I can't figure out
> what is wrong, I checked on google and I found a tutorial which is
> exactly what I have done (the tutorial =>
>
>
http://sleekd.com/general/adding-multiple-images-t...
> ).
>

I dont know the exact answer, but I would start with a debugger
breakpoint
in the controller create action and see what params you are getting, how
they are getting assigned to your model attributes (I guess you are
probably
using update_attributes), and this should at least give you more
information
on what's going on.

If still having trouble would not hurt to post your schema for pertinent
models and the model code pertaining to paperclip to see if there is
anything not matching up.
Ded91d26e72879bcd3c2929a71ec1d9b?d=identicon&s=25 Johndel Deliyiannis (johndel)
on 2011-05-28 03:35
(Received via mailing list)
It worked. I tried many things and now it uploads my images. Thank you
even though I didn't understand what was wrong.
4fe8a47d8188fb1fee703bd999250018?d=identicon&s=25 rails-pat (Guest)
on 2014-01-23 17:22
(Received via mailing list)
Hi everybody !

This topic is quite old, but since i was confronted to a similar
problem, i
thought it could help anybody interested in.

After consulting the rails API over nested attributes:
http://api.rubyonrails.org/classes/ActiveRecord/Ne...
, I solved the problem through allowing parameters in the controller.

Adapted to the given example, it should go like that :

File : app/controllers/girls_controller.rb
[...]
def girl_params

params.require(:girl).permit(:name,:surname,:height,:weight,:description,girl_photos_attributes:[:photo,:id,:_destroy])
    end

Happy Hacking !


Le samedi 28 mai 2011 03:34:18 UTC+2, Yiannis a écrit :
Please log in before posting. Registration is free and takes only a minute.
Existing account

NEW: Do you have a Google/GoogleMail, Yahoo or Facebook account? No registration required!
Log in with Google account | Log in with Yahoo account | Log in with Facebook account
No account? Register here.