Rails 5 个常量 & 模型 & 虚拟属性
Rails 5 Constant & Model & Virtual Attributes
我正在尝试在模型虚拟属性中使用预定义常量。
当我创建用户时,它显示绝对 avatar_url
并且工作正常。
问题:
当我在登录方法中找到用户时,它 return 只是相对的 url
即 "avatar_url": "/avatars/original/missing.png"
这意味着 #{SERVER_BASE_PATH}
在那一刻没有插入。
它也适用于某些 api
调用,即更新用户时。但并非在所有情况下。请帮我解决这个问题,以便在所有 api
次调用中获得绝对 url
示例:
型号:
class User < ApplicationRecord
# user model
# if avatar url is empty then use default image url
# SERVER_BASE_PATH is defined in config/initializer/constant.rb
attribute :avatar_url, :string, default: -> { "#{SERVER_BASE_PATH}/avatars/original/missing.png" }
end
控制器:
登录方式简单
class UsersController < ApplicationController
def login
response = OK()
unless params[:email].present? and params[:password].present?
render json: missing_params_specific('either user [email] or [password]') and return
end
response[:user] = []
response[:status] = '0'
begin
user = User.find_by(email: params[:email].downcase)
if user && user.authenticate(params[:password])
# following line first check if user profile image exist then it places that image url given by paperclip
# if not exist then url defined in model virtual attributes is used.
user.avatar_url = user.avatar.url if user.avatar.url.present?
user.set_last_sign_in_at user.sign_in_count
response[:user] = user
response[:status] = '1'
else
response[:message] = 'Invalid email/password combination' # Not quite right!
end
rescue => e
response[:message] = e.message
end
render json: response and return
end
end
API JSON 响应:
{
"JSON_KEY_STATUS_CODE": 1,
"JSON_KEY_STATUS_MESSAGE": "OK",
"server_time": 1490623384,
"user": {
"confirmed": true,
"user_status": "Active",
"admin": false,
"user_role": "Standard",
"first_name": "Super",
"last_name": "User",
"full_name": "Super User",
"avatar_url": "/avatars/original/missing.png", <-- Here (not absolute url)
"is_active": true,
},
"status": "1"
}
据我了解,JSON 响应中的 "avatar_url"
属性是您在与 Paperclip 集成的模型中定义的 default_url
:
class User < ActiveRecord::Base
has_attached_file :avatar,
styles: { medium: "300x300>", thumb: "100x100>" },
default_url: "/images/:style/missing.png"
validates_attachment_content_type :avatar, content_type: /\Aimage\/.*\z/
end
你试过用你 SERVER_BASE_PATH
设置 default_url
吗?
我正在尝试在模型虚拟属性中使用预定义常量。
当我创建用户时,它显示绝对 avatar_url
并且工作正常。
问题:
当我在登录方法中找到用户时,它 return 只是相对的 url
即 "avatar_url": "/avatars/original/missing.png"
这意味着 #{SERVER_BASE_PATH}
在那一刻没有插入。
它也适用于某些 api
调用,即更新用户时。但并非在所有情况下。请帮我解决这个问题,以便在所有 api
次调用中获得绝对 url
示例:
型号:
class User < ApplicationRecord
# user model
# if avatar url is empty then use default image url
# SERVER_BASE_PATH is defined in config/initializer/constant.rb
attribute :avatar_url, :string, default: -> { "#{SERVER_BASE_PATH}/avatars/original/missing.png" }
end
控制器:
登录方式简单
class UsersController < ApplicationController
def login
response = OK()
unless params[:email].present? and params[:password].present?
render json: missing_params_specific('either user [email] or [password]') and return
end
response[:user] = []
response[:status] = '0'
begin
user = User.find_by(email: params[:email].downcase)
if user && user.authenticate(params[:password])
# following line first check if user profile image exist then it places that image url given by paperclip
# if not exist then url defined in model virtual attributes is used.
user.avatar_url = user.avatar.url if user.avatar.url.present?
user.set_last_sign_in_at user.sign_in_count
response[:user] = user
response[:status] = '1'
else
response[:message] = 'Invalid email/password combination' # Not quite right!
end
rescue => e
response[:message] = e.message
end
render json: response and return
end
end
API JSON 响应:
{
"JSON_KEY_STATUS_CODE": 1,
"JSON_KEY_STATUS_MESSAGE": "OK",
"server_time": 1490623384,
"user": {
"confirmed": true,
"user_status": "Active",
"admin": false,
"user_role": "Standard",
"first_name": "Super",
"last_name": "User",
"full_name": "Super User",
"avatar_url": "/avatars/original/missing.png", <-- Here (not absolute url)
"is_active": true,
},
"status": "1"
}
据我了解,JSON 响应中的 "avatar_url"
属性是您在与 Paperclip 集成的模型中定义的 default_url
:
class User < ActiveRecord::Base
has_attached_file :avatar,
styles: { medium: "300x300>", thumb: "100x100>" },
default_url: "/images/:style/missing.png"
validates_attachment_content_type :avatar, content_type: /\Aimage\/.*\z/
end
你试过用你 SERVER_BASE_PATH
设置 default_url
吗?