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

时间:2019-04-21 17:00:54

标签: ruby-on-rails ruby-on-rails-5 url-routing

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

  1. 使用注册用户的电子邮件,“ redirect_url”作为参数来命中“ localhost:3000 / auth / password”。我也收到了电子邮件。

  2. 单击邮件中的“更改我的密码”链接时,它将重定向到“ localhost:3000 / api / auth / password / edit / ...”,而不是“ localhost:3000 / auth / password /编辑/...”

我的reset_password_instructions.html.erb如下:

class SearchCollectionViewModel {
    let name: String
    let previewURL: String?
    let dataController = DataController()
    var image: UIImage?

    init(with result: Result) {
        self.name = result.trackName
        self.previewURL = result.previewURL
    }
}

extension SearchCollectionViewModel {
    func preview(with url: String, completion: @escaping (UIImage?) -> Void) {
        guard let urlString = previewURL, let url = URL(string: urlString) else {
            completion(nil)
            return
        }

        dataController.downloadImage(with: url) { [weak self] image, error in
            DispatchQueue.main.async {
                self?.image = image
                completion(image)
            }
        }
    }
}

我无法弄清楚“ edit_password_url”如何解析为“ localhost:3000 / api / auth / password / edit / ...”而不是“ localhost:3000 / auth / password / edit / ...” 。有人可以在这里帮助我吗?

注意:在development.rb中,我有<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>

更新:

Routes.rb

config.action_controller.default_url_options = { host: 'localhost:3000' }

用于devise_token_auth的耙路输出:

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

Swagger_docs.rb

                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

结束

1 个答案:

答案 0 :(得分:1)

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

config.relative_url_root = "/api"

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

config.relative_url_root = "/app1"

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

现在,在生成链接时,路轨将在“ / app1”之前添加

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