DigitalOcean上Rails生产环境日志与Flash不显示问题求助
Rails生产环境(DigitalOcean)异常排查问题
问题现象
- 本地Mac开发环境:无论API响应状态码是200还是其他值,日志与Flash提示均正常工作。例如上传PNG文件(API仅支持JPG)时,错误信息会通过Flash展示,日志也能正常输出对应内容。
- DigitalOcean生产环境:正常请求(如上传JPG)无问题,但上传PNG这类触发API非200响应的错误请求时,仅会触发重定向操作,控制器中的Log1(API调用前的日志)、Log3(API非200响应时的日志)均未输出,只有Log2始终正常显示。从现象看,Log1之后的代码似乎没有执行,但成功请求时这部分代码运行完全正常。
已排查内容
已排除session数据异常问题,结合日志无输出的情况,推测并非Flash组件本身的问题。
核心代码
控制器上传方法代码
def upload Rails.logger.debug "========= Log 1}" if params[:file].blank? Rails.logger.debug "========= Log 2}" flash[:alert] = "Please upload a file before clicking Analyze." return redirect_to skin_analyze_path end @uploaded_file = params[:file] @response = skin_analyze(@uploaded_file) if @response.code != 200 Rails.logger.debug "========= Log 3}" json_response = JSON.parse(@response.body) flash[:alert] = json_response["error_msg"] || json_response["message"] redirect_to skin_analyze_path, data: { turbo: false } end ... end
触发请求的表单代码
<%= form_with(url: picture_upload_path, method: :post, local: true, html: { multipart: true, id: 'uploadForm', class: 'mt-4' }) do |form| %> <div class="col-12 mb-4"> <% is_disabled = @user.pictures.attached? && !@user.is_paying_customer? %> <%= form.submit is_disabled ? "Upgrade to Pro for unlimited access" : "Analyze", type: "submit", class: "btn btn-primary w-100", id: "analyze", disabled: is_disabled, data: { disable_with: "Analyzing..." } %> </div> <% end %>
内容的提问来源于stack exchange,提问作者bonaparten
相关产品推荐
相关产品推荐

