在 Rails 5 中解析 url 时,如何将“/api”附加到 "localhost:3000"?
How "/api" is getting appended to "localhost:3000" when resolving url in Rails 5?
我正在尝试在 Rails 5 api 中使用 "devise_token_auth" 实现 "Forgot Password" 功能,其中我执行了以下步骤:
点击"localhost:3000/auth/password"注册用户邮箱,redirect_url作为参数。我也收到了邮件。
当点击邮件中的 "change my password" link 时,它重定向到 "localhost:3000/api/auth/password/edit/..." 而不是 "localhost:3000/auth/password/edit/..."
我的reset_password_instructions.html.erb如下:
<p><%= t(:hello).capitalize %> <%= @resource.email %>!</p>
<p><%= t '.request_reset_link_msg' %></p>
<p><%= link_to t('.password_change_link'), edit_password_url(@resource, reset_password_token: @token, config: message['client-config'].to_s, redirect_url: message['redirect-url'].to_s).html_safe %></p>
<p><%= t '.ignore_mail_msg' %></p>
<p><%= t '.no_changes_msg' %></p>
我无法弄清楚 "edit_password_url" 如何解析为 "localhost:3000/api/auth/password/edit/..." 而不是 "localhost:3000/auth/password/edit/..."。有人能帮我吗?
注意:在development.rb中,我有config.action_controller.default_url_options = { host: 'localhost:3000' }
更新:
Routes.rb
Rails.application.routes.draw do
#.....
resources :employee_contact_infos
resources :employee_work_infos
mount_devise_token_auth_for 'User', at: 'auth'
# get '/api' => redirect('/swagger/dist/index.html?url=/apidocs/api-docs.json')
# For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html
get '/industry_sectors' => 'drop_down_values#industry_sector_list'
get '/currencies' => 'drop_down_values#currency'
#...
namespace :employees do
end
end
devise_token_auth 的 rake 路由输出:
new_user_session GET /auth/sign_in(.:format) devise_token_auth/sessions#new
user_session POST /auth/sign_in(.:format) devise_token_auth/sessions#create
destroy_user_session DELETE /auth/sign_out(.:format) devise_token_auth/sessions#destroy
new_user_password GET /auth/password/new(.:format) devise_token_auth/passwords#new
edit_user_password GET /auth/password/edit(.:format) devise_token_auth/passwords#edit
user_password PATCH /auth/password(.:format) devise_token_auth/passwords#update
PUT /auth/password(.:format) devise_token_auth/passwords#update
POST /auth/password(.:format) devise_token_auth/passwords#create
cancel_user_registration GET /auth/cancel(.:format) devise_token_auth/registrations#cancel
new_user_registration GET /auth/sign_up(.:format) devise_token_auth/registrations#new
edit_user_registration GET /auth/edit(.:format) devise_token_auth/registrations#edit
user_registration PATCH /auth(.:format) devise_token_auth/registrations#update
PUT /auth(.:format) devise_token_auth/registrations#update
DELETE /auth(.:format) devise_token_auth/registrations#destroy
POST /auth(.:format) devise_token_auth/registrations#create
auth_validate_token GET /auth/validate_token(.:format) devise_token_auth/token_validations#validate_token
Swagger_docs.rb
class Swagger::Docs::Config
def self.base_api_controller; ApplicationController end
def self.transform_path(path, api_version)
# Make a distinction between the APIs and API documentation paths.
"#{path}"
end
Swagger::Docs::Config.register_apis({
'1.0' => {
controller_base_path: '',
api_file_path: 'public/apidocs',
base_path: 'http://localhost:3000',
clean_directory: true
}
})
结束
如果设置了相对 url 根目录的任何配置,则生成的 link 会发生变化。
你能检查一下你是否在任何配置中定义设置相对 url root 如下:
config.relative_url_root = "/api"
假设您要将应用程序部署到“/app1”。 Rails 需要知道此目录才能生成适当的路由。
config.relative_url_root = "/app1"
或者您可以设置 RAILS_RELATIVE_URL_ROOT 环境变量。
Rails 现在会在生成 links
时添加“/app1”
检查 - https://guides.rubyonrails.org/configuring.html#deploy-to-a-subdirectory-relative-url-root
我正在尝试在 Rails 5 api 中使用 "devise_token_auth" 实现 "Forgot Password" 功能,其中我执行了以下步骤:
点击"localhost:3000/auth/password"注册用户邮箱,redirect_url作为参数。我也收到了邮件。
当点击邮件中的 "change my password" link 时,它重定向到 "localhost:3000/api/auth/password/edit/..." 而不是 "localhost:3000/auth/password/edit/..."
我的reset_password_instructions.html.erb如下:
<p><%= t(:hello).capitalize %> <%= @resource.email %>!</p>
<p><%= t '.request_reset_link_msg' %></p>
<p><%= link_to t('.password_change_link'), edit_password_url(@resource, reset_password_token: @token, config: message['client-config'].to_s, redirect_url: message['redirect-url'].to_s).html_safe %></p>
<p><%= t '.ignore_mail_msg' %></p>
<p><%= t '.no_changes_msg' %></p>
我无法弄清楚 "edit_password_url" 如何解析为 "localhost:3000/api/auth/password/edit/..." 而不是 "localhost:3000/auth/password/edit/..."。有人能帮我吗?
注意:在development.rb中,我有config.action_controller.default_url_options = { host: 'localhost:3000' }
更新:
Routes.rb
Rails.application.routes.draw do
#.....
resources :employee_contact_infos
resources :employee_work_infos
mount_devise_token_auth_for 'User', at: 'auth'
# get '/api' => redirect('/swagger/dist/index.html?url=/apidocs/api-docs.json')
# For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html
get '/industry_sectors' => 'drop_down_values#industry_sector_list'
get '/currencies' => 'drop_down_values#currency'
#...
namespace :employees do
end
end
devise_token_auth 的 rake 路由输出:
new_user_session GET /auth/sign_in(.:format) devise_token_auth/sessions#new
user_session POST /auth/sign_in(.:format) devise_token_auth/sessions#create
destroy_user_session DELETE /auth/sign_out(.:format) devise_token_auth/sessions#destroy
new_user_password GET /auth/password/new(.:format) devise_token_auth/passwords#new
edit_user_password GET /auth/password/edit(.:format) devise_token_auth/passwords#edit
user_password PATCH /auth/password(.:format) devise_token_auth/passwords#update
PUT /auth/password(.:format) devise_token_auth/passwords#update
POST /auth/password(.:format) devise_token_auth/passwords#create
cancel_user_registration GET /auth/cancel(.:format) devise_token_auth/registrations#cancel
new_user_registration GET /auth/sign_up(.:format) devise_token_auth/registrations#new
edit_user_registration GET /auth/edit(.:format) devise_token_auth/registrations#edit
user_registration PATCH /auth(.:format) devise_token_auth/registrations#update
PUT /auth(.:format) devise_token_auth/registrations#update
DELETE /auth(.:format) devise_token_auth/registrations#destroy
POST /auth(.:format) devise_token_auth/registrations#create
auth_validate_token GET /auth/validate_token(.:format) devise_token_auth/token_validations#validate_token
Swagger_docs.rb
class Swagger::Docs::Config
def self.base_api_controller; ApplicationController end
def self.transform_path(path, api_version)
# Make a distinction between the APIs and API documentation paths.
"#{path}"
end
Swagger::Docs::Config.register_apis({
'1.0' => {
controller_base_path: '',
api_file_path: 'public/apidocs',
base_path: 'http://localhost:3000',
clean_directory: true
}
})
结束
如果设置了相对 url 根目录的任何配置,则生成的 link 会发生变化。 你能检查一下你是否在任何配置中定义设置相对 url root 如下:
config.relative_url_root = "/api"
假设您要将应用程序部署到“/app1”。 Rails 需要知道此目录才能生成适当的路由。
config.relative_url_root = "/app1"
或者您可以设置 RAILS_RELATIVE_URL_ROOT 环境变量。
Rails 现在会在生成 links
时添加“/app1”检查 - https://guides.rubyonrails.org/configuring.html#deploy-to-a-subdirectory-relative-url-root