簡體   English   中英

在導軌中使用REGEX時是否動態分配常量ApplicationController?

[英]Dynamic constant assignment when using REGEX inside rails ApplicationController?

我需要將我的base64圖像字符串轉換為實際文件,然后上傳到服務器。

但是似乎當我嘗試通過使用REGEX rails控制器解碼base64文件時卻不允許我這樣做。

這是我的控制器

def update_with_image
    user = current_user

    REGEXP = /\Adata:([-\w]+\/[-\w\+\.]+)?;base64,(.*)/m


    data_uri_parts = user_update_params[:profile_image].match(REGEXP) || []
    extension = MIME::Types[data_uri_parts[1]].first.preferred_extension
    file_name = "user_profile_image_#{user.id}.#{extension}"

    File.open(file_name, 'wb') do |file|
      file.write(Base64.decode64(data_uri_parts[2]))
    end

    uploader = PictureUploader.new
    uploader.store!(file_name)
    user_update_params[:profile_image] = uploader.url

    if user.update_attributes(user_update_params)
      # Handle a successful update.
      render json: user, status: 200 ,serializer: UserSerializer
    else
      render json: { errors: user.errors }, status: 422
    end
end

這是我從服務器得到的

"#<SyntaxError: /Sites/xxx/users_controller.rb:57: dynamic constant assignment↵    REGEXP = /\Adata:([-\w]+\/[-\w\+\.]+)?;base64,(.*)/m↵`            ^>"

那么我該如何解決呢?

謝謝!

現在我只是不使用變量就直接傳遞了表達式,並且效果很好。 –用戶3403614

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM