我试图在模型虚拟属性中使用预定义常量。
当我创建用户时,它显示绝对avatar_url并且工作正常。
问题:
当我在登录方法中找到用户时,它只返回相对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 endAPI 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" }I am trying to use predefined constant in Model virtual attributes.
when I create user, it shows absolute avatar_url and works fine.
Problem:
when I find user in login method it return only relative url i.e "avatar_url": "/avatars/original/missing.png" which means #{SERVER_BASE_PATH} is not interpolating at that moment.
It also works fine with some api call i.e when updating user. but not in all cases. please help me how can I fix this problem in order to get absolute url in all api calls
Example:
Model:
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" } endController:
it has simple login method
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 endAPI JSON Response:
{ "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"属性是您在模型上定义的default_url ,您将Paperclip与以下内容集成:
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 ?
From what I understand, the "avatar_url" attribute on your JSON response is the default_url that you define on your model that you integrate Paperclip with:
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/ endDid you try to set the default_url with you SERVER_BASE_PATH?
Rails 5常量和模型和虚拟属性(Rails 5 Constant & Model & Virtual Attributes)我试图在模型虚拟属性中使用预定义常量。
当我创建用户时,它显示绝对avatar_url并且工作正常。
问题:
当我在登录方法中找到用户时,它只返回相对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 endAPI 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" }I am trying to use predefined constant in Model virtual attributes.
when I create user, it shows absolute avatar_url and works fine.
Problem:
when I find user in login method it return only relative url i.e "avatar_url": "/avatars/original/missing.png" which means #{SERVER_BASE_PATH} is not interpolating at that moment.
It also works fine with some api call i.e when updating user. but not in all cases. please help me how can I fix this problem in order to get absolute url in all api calls
Example:
Model:
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" } endController:
it has simple login method
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 endAPI JSON Response:
{ "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"属性是您在模型上定义的default_url ,您将Paperclip与以下内容集成:
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 ?
From what I understand, the "avatar_url" attribute on your JSON response is the default_url that you define on your model that you integrate Paperclip with:
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/ endDid you try to set the default_url with you SERVER_BASE_PATH?
发布评论