my plan is to use Devise to register a user and giving him the opportunity to upload a picture (paperclip) while registering.
So I have users and users_pictures where user has_many :users_pictures.
I modified the usermodel like this:
after_create :create_users_picture
def create_users_picture
user_id = User.last.user_id
UsersPicture.create!(:user_id => user_id)
end
This was just a test to see if it’s working and it actually does.
But now I need to get the fileinformation from the paperclip upload.
I already inserted the file_field to the users_view and the data is submitted correctly.
Then I tried something like:
def create_users_picture
@users_picture = UsersPicture.new(@user)
@users_picture.save
end
Because I thought that @user contains all necessary data … but then I get:
Parameters: {"user"=>{"user_firstname"=>"merrr", "password_confirmation"=>"[FILTERED]", "photo"=>#<ActionDispatch::Http::UploadedFile:0xe54abbb4 @content_type="image/jpeg", @tempfile=#<File:/tmp/RackMultipart20121023-7937-1tbp5v7-0>, @headers="Content-Disposition: form-data; name=\"user[photo]\"; filename=\"rh.jpeg\"\r\nContent-Type: image/jpeg\r\n", @original_filename="rh.jpeg">, "user_lastname"=>"merrr", "email"=>"merrr@asd.de", "password"=>"[FILTERED]", "user_login"=>"merrr"}, "commit"=>"Sign up", "authenticity_token"=>"GRzc3Wx5mJJwBL32qjpMi24sCZdoWdRZgucldmk7Bmo=", "utf8"=>"✓"}
Completed 500 Internal Server Error in 347ms
ActiveRecord::StatementInvalid (Mysql2::Error: Column 'user_id' cannot be null: INSERT INTO `users_pictures` (`photo_content_type`, `photo_file_name`, `photo_file_size`, `user_id`) VALUES (NULL, NULL, NULL, NULL)):
app/models/user.rb:36:in `create_user_profile'
So I can get the user_id by using User.last.user_id but how can I get the fileinformation from the picture upload ?
Do I need to get access to the POST data ? And if so, how do I do that ?
Additionally, I need to check if the user eventually tried to upload a picture … because he does not have to.
That could be done by simply checking if there was some kind of picture data send by the form ?
Thanks in advance 🙂
I don’t think that you actually understand that you are doing))
Good way to do that you want is to use nested attributes for association.
But i don’t thin it will help you, so first of all you have to read everything here