1

So I'm working with carrier wave direct, and I'm at the point where I need to update the "key" attribute on the uploader object. Here's the relevant line on the docs:

After uploading to S3, You'll need to update the uploader object with the returned key in the controller action that corresponds to new_user_url:

@uploader.update_attribute :key, params[:key]

The problem is that my @uploader object doesn't have an update_attribute method. In fact, when I look at all of the methods on the @uploader object, I see methods like key() and key=(), but no update_attribute.

Any idea what's going on? Did I miss some setup step that I need to perform to make the update_attribute method available?

jyli7
  • 2,731
  • 6
  • 23
  • 31

2 Answers2

7

I got it to work by calling update_attribute on the model and not the uploader. In the below snipped the @uploader the subclass of CarrierWave::Uploader::Base and @video is the model.

  def upload
    @uploader = Video.new.asset
    @uploader.success_action_redirect = videos_upload_successful_url
  end

  def upload_successful
    @video = Video.new
    @video.update_attribute :key, params[:key] # different than documentation!!
    @video.save
  end

This seems to be contrary to the documentation where it is documented they way you tried it.

Bruce Chu
  • 216
  • 3
  • 5
0

I had an same issue, but I solved the problem to add new column. I think you need to add another column like avatar_image_url aside from avatar which is used as mount_uploader :avatar, AvatarUploader.

And the controller is something like this:

class ProfilesController < ApplicationController
  before_action :setup_context

  def edit
    @uploader = @profile.avatar
    @uploader.success_action_status = '201'
  end

  def update
    if @profile.update_attributes(profile_params)
      redirect_to home_path
    else
      render :edit
    end
  end

  private

  def setup_context
    @profile = current_user
  end

  def profile_params
    params.require(:user).permit(:name, :avatar_image_url)
  end

end

When you post the following parameters, you can save avatar_image_url.

Parameters: {"utf8"=>"✓", "user"=>{"avatar_image_url"=>"https://my-development.s3.amazonaws.com/uploads/220f5378-1e0f-4823-9527-3d1170089a49/foo.gif", "name"=>"Schneems"}}

You can refer the image like this.

<%= image_tag @user.avatar_image_url %>

And also you can check this out.

Direct to S3 Image Uploads in Rails | Heroku Dev Center

jwako
  • 173
  • 2
  • 12