Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails/Devise: Nil location provided. Can't build URI. on Password reset

I'm using Rails 4.2 and Devise. Whenever I try to use the reset password page I get the following error:

Nil location provided. Can't build URI. on Password reset

In production it comes with a 500 internal server error.

The email with the reset token does actually send. And if user opens the link in their email they can return to the site to change the password. Despite this, the 500 error will always arise when the 'reset password button' is clicked.

I've used devise many times before, and have never had this issue. I suspected it might have something to do following this tutorial:

https://github.com/plataformatec/devise/wiki/How-To:-Display-a-custom-sign_in-form-anywhere-in-your-app

and explicitly writing the resource_name, resource and devise_mapping methods in my application controller, yet they haven't interfered with any other devise route, and even after commenting them out, the error has persisted.

Edit:

I don't have a PasswordController just sticking to most of the default settings of Devise. Nor am I using respond_with anywhere in my app.

My Devise Model looks like this:

class User < ActiveRecord::Base
    devise :database_authenticatable, :registerable, :confirmable, :lockable,
     :recoverable, :rememberable, :trackable, :validatable, 
     :omniauthable, :omniauth_providers => [:stripe_connect]


    validates :username, presence: true
    validates :time_zone, presence: true
    has_one :teacher, dependent: :destroy
    has_many :bookings, foreign_key: :student_id
    has_many :reviews, foreign_key: :student_id, dependent: :destroy
    has_many :messages, dependent: :destroy
    VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i
    validates_format_of :email, with: VALID_EMAIL_REGEX

end

Edit 3:

Here are the relevant routes:

get 'sessions/create'
devise_for :users, :controllers => { :omniauth_callbacks => "omniauth_callbacks" }
resources :users, :only =>[:show]

devise_scope :user do
    get 'bookings', to: 'bookings#bookings'
end

Here is the view:

<%= form_for(resource, :as => resource_name, :url => password_path(resource_name), :html => { :method => :post, :class => "sky-form" }) do |f| %>
  <%= devise_error_messages! %>
  <h3 class="text-center"><%= t('.header') %></h3>
  <section>
    <div class="row">
      <label class="label col col-4"><%= t('.email') %></label>
        <div class="col col-8">
          <label class="input">
            <%= f.email_field :email %>
          </label>
        </div>
      </div>
    </section>
    <footer class="text-right">
      <%= f.submit t('.resetpwbtn'), class: "btn btn-theme-bg btn-lg" %>
    </footer>
  <% end %>
like image 851
dedles Avatar asked Oct 18 '25 20:10

dedles


2 Answers

This can happen with devise when using the new turbo/hotwire.

In my case I customized the forms to set "data-turbo": "false"

Like so:

<%= form_for(resource, as: resource_name, url: confirmation_path(resource_name), html: { "data-turbo": "false", method: :post }) do |f| %>

If you haven't done so, you will need to use Devise custom views: https://dev.to/n350071/customize-view-of-devise-3k95

like image 77
randomcontrol Avatar answered Oct 20 '25 11:10

randomcontrol


If you are using respond_with it can only be used with resource. I've had that problem and the fix was to you render.

If this is the case something like

render :text => 'false'

could possibly help. But to be sure we would have to see your model to figure out more of the issue.

The other issue, could be that responds_with expects an ActiveRecord object for it to map out the routes.

I am not exactly sure how it is implemented within your code, but these are some ways that I have solved this error in the past.

like image 34
Joe Marion Avatar answered Oct 20 '25 10:10

Joe Marion