部署 ruby china 部署成功后的问题,帖子详情页 500,更换头像后不能显示

es6china · 2017年07月21日 · 最后由 es6china 回复于 2017年07月22日 · 1771 次阅读

服务器为阿里云的 ubuntu 服务器 ~

环境如下:

Rails 5.1.1
ruby 2.4.1
ubuntu 14.04
Postgresql 9.3
Passenger 5.1.6

nginx 配置如下:

server {
    listen 80;
    server_name 域名;
    root /home/homeland/public;
    index  index.html index.htm;
    passenger_enabled on;
}

production.log 报错信息如下:

F, [2017-07-21T21:50:02.176570 #2842] FATAL -- :   
F, [2017-07-21T21:50:02.176598 #2842] FATAL -- : app/controllers/application_controller.rb:73:in `block (2 levels) in render_optional_error_file'
app/controllers/application_controller.rb:71:in `render_optional_error_file'
app/controllers/application_controller.rb:60:in `render_404'
app/controllers/home_controller.rb:7:in `uploads'
I, [2017-07-21T21:50:02.177873 #2842]  INFO -- : Started GET "/uploads/user/avatar/1/88363d.jpg!sm" for 171.216.71.44 at 2017-07-21 21:50:02 +0800
I, [2017-07-21T21:50:02.179074 #2842]  INFO -- : Processing by HomeController#uploads as 
I, [2017-07-21T21:50:02.179145 #2842]  INFO -- :   Parameters: {"path"=>"user/avatar/1/88363d"}
I, [2017-07-21T21:50:02.183168 #2842]  INFO -- : Completed 500 Internal Server Error in 4ms (ActiveRecord: 0.3ms)
F, [2017-07-21T21:50:02.183918 #2842] FATAL -- :   
F, [2017-07-21T21:50:02.183983 #2842] FATAL -- : ActionView::MissingTemplate (Missing template home/uploads, application/uploads with {:locale=>[:"zh-CN", :zh], :formats=>[nil], :variants=>[], :handlers=>[:raw, :erb, :html, :builder, :ruby, :coffee, :jbuilder]}. Searched in:
  * "/home/homeland/app/views"
  * "/home/deploy/.rvm/gems/ruby-2.4.1/gems/homeland-site-0.1.2/app/views"
  * "/home/deploy/.rvm/gems/ruby-2.4.1/gems/homeland-note-0.1.3/app/views"
  * "/home/deploy/.rvm/gems/ruby-2.4.1/gems/homeland-wiki-0.3.0/app/views"
  * "/home/deploy/.rvm/gems/ruby-2.4.1/gems/homeland-press-0.3.4/app/views"
  * "/home/deploy/.rvm/gems/ruby-2.4.1/gems/status-page-0.1.4/app/views"
  * "/home/deploy/.rvm/gems/ruby-2.4.1/gems/exception-track-0.2.0/app/views"
  * "/home/deploy/.rvm/gems/ruby-2.4.1/gems/kaminari-core-1.0.1/app/views"
  * "/home/deploy/.rvm/gems/ruby-2.4.1/gems/notifications-0.4.2/app/views"
  * "/home/deploy/.rvm/gems/ruby-2.4.1/gems/devise-4.3.0/app/views"
  * "/home/deploy/.rvm/gems/ruby-2.4.1/gems/doorkeeper-4.2.6/app/views"
  * "/home/deploy/.rvm/gems/ruby-2.4.1/gems/pghero-1.7.0/app/views"
):
app/controllers/application_controller.rb:71:in `render_optional_error_file'
app/controllers/application_controller.rb:60:in `render_404'
app/controllers/home_controller.rb:7:in `uploads'

你把这两个文件贴一下。

application_controller 文件信息如下:

# ApplicationController
class ApplicationController < ActionController::Base
  protect_from_forgery prepend: true
  helper_method :unread_notify_count
  helper_method :turbolinks_app?, :turbolinks_ios?, :turbolinks_app_version

  # Addition contents for etag
  etag { current_user.try(:id) }
  etag { unread_notify_count }
  etag { flash }
  etag { Setting.navbar_html }
  etag { Setting.footer_html }
  etag { Rails.env.development? ? Time.now : Date.current }

  before_action do
    resource = controller_name.singularize.to_sym
    method = "#{resource}_params"
    params[resource] &&= send(method) if respond_to?(method, true)

    if devise_controller?
      devise_parameter_sanitizer.permit(:sign_in) { |u| u.permit(*User::ACCESSABLE_ATTRS) }
      devise_parameter_sanitizer.permit(:account_update) do |u|
        if current_user.email_locked?
          u.permit(*User::ACCESSABLE_ATTRS)
        else
          u.permit(:email, *User::ACCESSABLE_ATTRS)
        end
      end
      devise_parameter_sanitizer.permit(:sign_up) { |u| u.permit(*User::ACCESSABLE_ATTRS) }
    end

    User.current = current_user
    cookies.signed[:user_id] ||= current_user.try(:id)

    # hit unread_notify_count
    unread_notify_count
  end

  before_action :set_active_menu
  def set_active_menu
    @current = case controller_name
               when "pages"
                 ["/wiki"]
               else
                 ["/#{controller_name}"]
               end
  end

  before_action :set_locale
  def set_locale
    I18n.locale = user_locale

    # after store current locale
    cookies[:locale] = params[:locale] if params[:locale]
  rescue I18n::InvalidLocale
    I18n.locale = I18n.default_locale
  end

  def render_404
    render_optional_error_file(404)
  end

  def render_403
    render_optional_error_file(403)
  end

  def render_optional_error_file(status_code)
    status = status_code.to_s
    fname = %w(404 403 422 500).include?(status) ? status : "unknown"

    respond_to do |format|
      format.html { render template: "/errors/#{fname}", handler: [:erb], status: status, layout: "application" }
      format.all  { render nothing: true, status: status }
    end
  end

  rescue_from CanCan::AccessDenied do |_exception|
    redirect_to main_app.root_path, alert: t("common.access_denied")
  end

  def store_location
    session[:return_to] = request.url
  end

  def redirect_back_or_default(default)
    redirect_to(session[:return_to] || default)
    session[:return_to] = nil
  end

  def redirect_referrer_or_default(default)
    redirect_to(request.referrer || default)
  end

  def unread_notify_count
    return 0 if current_user.blank?
    @unread_notify_count ||= Notification.unread_count(current_user)
  end

  def authenticate_user!(opts = {})
    return if current_user
    if turbolinks_app?
      render plain: "401 Unauthorized", status: 401
      return
    end

    store_location

    super(opts)
  end

  def current_user
    if doorkeeper_token
      return @current_user if defined? @current_user
      @current_user ||= User.find_by_id(doorkeeper_token.resource_owner_id)
      sign_in @current_user
      @current_user
    else
      super
    end
  end

  def turbolinks_app?
    @turbolinks_app ||= request.user_agent.to_s.include?("turbolinks-app")
  end

  def turbolinks_ios?
    @turbolinks_ios ||= turbolinks_app? && request.user_agent.to_s.include?("iOS")
  end

  # read turbolinks app version
  # example: version:2.1
  def turbolinks_app_version
    return "" unless turbolinks_app?
    return @turbolinks_app_version if defined? @turbolinks_app_version
    version_str = request.user_agent.to_s.match(/version:[\d\.]+/).to_s
    @turbolinks_app_version = version_str.split(":").last
    @turbolinks_app_version
  end

  # Require Setting enabled module, else will render 404 page.
  def self.require_module_enabled!(name)
    before_action do
      unless Setting.has_module?(name)
        render_404
      end
    end
  end

  def require_no_sso!
    redirect_to auth_sso_path if Setting.sso_enabled?
  end

  private

  def user_locale
    params[:locale] || cookies[:locale] || http_head_locale || Setting.default_locale || I18n.default_locale
  end

  def http_head_locale
    return nil if Setting.auto_locale == false
    http_accept_language.language_region_compatible_from(I18n.available_locales)
  end
end

home_controller 文件信息:

class HomeController < ApplicationController
  def index
    @excellent_topics = Topic.excellent.recent.fields_for_list.limit(20).to_a
  end

  def uploads
    return render_404 if Rails.env.production?

    # This is a temporary solution for help generate image thumb
    # that when you use :file upload_provider and you have no Nginx image_filter configurations.
    # DO NOT use this in production environment.
    format, version = params[:format].split("!")
    filename = [params[:path], format].join(".")
    pragma = request.headers["Pragma"] == "no-cache"
    thumb = Homeland::ImageThumb.new(filename, version, pragma: pragma)
    if thumb.exists?
      send_file thumb.outpath, type: "image/jpeg", disposition: "inline"
    else
      render plain: "File not found", status: 404
    end
  end

  def api
    redirect_to "/api-doc/"
  end

  def error_404
    render_404
  end

  def markdown
  end
end

chenge 回复

贴了,在上面~~

def uploads
    return render_404 if Rails.env.production?

我有点没看懂,为何这里会去 404。

不过等他们专家来答复吧,代码就是他们写的,好办。

chenge 回复

噗 好嘛~~😂 😂 😂 😂

帖子错误问题已解决,头像问题还在折腾中

es6china 关闭了讨论。 07月22日 13:40
es6china 重新开启了讨论。 07月23日 23:12
es6china 关闭了讨论。 07月23日 23:13
需要 登录 后方可回复, 如果你还没有账号请 注册新账号