在Rails 5中解析url时,如何将“/ api”附加到“localhost:3000”?

问题描述 投票:0回答:1

我正在尝试使用Rails 5 api中的“devise_token_auth”实现“忘记密码”功能,其中我执行了以下步骤:

  1. 点击“localhost:3000 / auth / password”,注册用户发送电子邮件,redirect_url作为参数。我也收到了电子邮件。
  2. 单击邮件中的“更改我的密码”链接时,它会重定向到“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
                                          }


                        })

结束

ruby-on-rails ruby-on-rails-5 url-routing
1个回答
1
投票

如果已设置相对URL根的任何配置,则生成的链接将更改。您可以检查是否已在任何配置中定义以设置相对URL根,如下所示:

config.relative_url_root = "/api"

假设您要将应用程序部署到“/ app1”。 Rails需要知道这个目录以生成适当的路由。

config.relative_url_root = "/app1"

或者,您可以设置RAILS_RELATIVE_URL_ROOT环境变量。

Rails现在会在生成链接时添加“/ app1”

检查 - https://guides.rubyonrails.org/configuring.html#deploy-to-a-subdirectory-relative-url-root

© www.soinside.com 2019 - 2024. All rights reserved.