Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Assigning photo attribute saves as ActionDispatch::Http:UploadedFile #2000

Closed
quantumpotato opened this issue Aug 11, 2016 · 5 comments
Closed

Comments

@quantumpotato
Copy link

I'm trying to save a file with carrierwave but when I try to read the attribute, I get an ActionDispatch::Http:UploadedFile object and can't access the file url.

irb(main):009:0> t.photo.url
NoMethodError: undefined method `url' for "#<ActionDispatch::Http::UploadedFile:0x007f77a09182e0>":String

Ticket.rb

class Ticket < ActiveRecord::Base
  mount_uploader :photo, PhotoUploader
end

My action:

def create
    @user = User.new(user_params)

    if @user.save       
      puts "Saved the user"
        @ticket = Ticket.new(ticket_parameters)
      image_data = params[:photo]
      @ticket.photo = image_data

And my upload:

- (void)registerUserWithTicket:(HDTicket *)t driver:(HDDriver *)d {
    NSString *endpoint = @"register.json";
    NSDictionary *params = @{@"ticket" : [t params], @"user" : [d params]};

    NSString *path = [self fullPath:endpoint];
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    manager.requestSerializer = [AFJSONRequestSerializer serializer];

    NSData *imageData = UIImagePNGRepresentation([self imageWithImage:t.photo convertToSize:CGSizeMake(t.photo.size.width / 4, t.photo.size.width / 4)]);

    [manager POST:path parameters:params constructingBodyWithBlock:^(id<AFMultipartFormData>  _Nonnull formData) {
        [formData appendPartWithFileData:imageData name:@"photo" fileName:@"image.png" mimeType:@"image/png"];
    } progress:^(NSProgress * _Nonnull uploadProgress) {

    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        [self.delegate registered:true];
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        [self.delegate registered:false];

    }];
@ghost
Copy link

ghost commented Aug 16, 2016

+1

1 similar comment
@sekretarot
Copy link

+1

@jamesalbuquerque
Copy link

Same problem.

@quantumproducer
Copy link

I'm glad I'm not the only one with this issue. Devs, please take a look!

@mshibuya
Copy link
Member

I guess something is messed up in your code(note that "#<ActionDispatch::Http::UploadedFile:0x007f77a09182e0>":String is string representation of UploadedFile, not an instance of UploadedFile).
If you could show me full model and uploader code, I can look into them.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants