2014-12-25 32 views
0

我在使用載波在RoR上載圖像時遇到問題。每當我試着上傳文件我得到這個錯誤:載波多部分編碼錯誤

You tried to assign a String or a Pathname to an uploader, for security reasons, this is not allowed. If this is a file upload, please check that your upload form is multipart encoded.

用戶#編輯:

= form_for(@user, multipart: true) do |f| 
    =f.file_field :avatar 
    = f.submit class: "btn btn-primary btn-block btn-lg" 

users_controller:

def update 
respond_to do |format| 
    if @user.update(user_params) 
    if params[:user][:avatar] 
     @user.avatar = params[:user][:avatar].tempfile.path 
     @user.save! 
    end 
    format.html { redirect_to @user, notice: 'User was successfully updated.' } 
    format.json { head :no_content } 
    else 
    format.html { render action: 'edit' } 
    format.json { render json: @user.errors, status: :unprocessable_entity } 
    end 
end 
end 

我在user.rb

mount_uploader :avatar, AvatarUploader

任何想法可能會出錯?

回答

0

您在控制器中的角色字段中分配一個字符串,以便引發錯誤。

控制器代碼shoudl是

def update 
    respond_to do |format| 
    ... 
      @user.avatar = params[:user][:avatar] 
      @user.save! 
     end 
    ... 
    end 

所以它應該是不@user.avatar = params[:user][:avatar]其是字符串

的文件路徑