我正在尝试在Devise的Reset Password页面上实现Ajax。提交的电子邮件很好,但我无法显示任何javascript响应,而且还出现ArgumentError (Nil location provided. Can't build URI.)错误

这是我从航站楼得到的东西:

Devise::Mailer#reset_password_instructions: processed outbound mail in 18.7ms

Sent mail to [email protected] (458.2ms)
Date: Mon, 20 Jun 2016 19:06:57 +0100
From: [email protected]
Reply-To: [email protected]
To: [email protected]
Message-ID: <[email protected]>
Subject: Reset password instructions
Mime-Version: 1.0
Content-Type: text/html;
 charset=UTF-8
Content-Transfer-Encoding: 7bit

<p>Hello [email protected]!</p>

<p>Someone has requested a link to change your password. You can do this through the link below.</p>

<p><a href="http://localhost:3000/users/password/edit?reset_password_token=6SuAqWSBnKiYZETDdaEJ">Change my password</a></p>

<p>If you didn't request this, please ignore this email.</p>
<p>Your password won't change until you access the link above and create a new one.</p>

Redirected to
Completed 500 Internal Server Error in 532ms (ActiveRecord: 31.6ms)

ArgumentError (Nil location provided. Can't build URI.):
  actionpack (4.2.5.2) lib/action_dispatch/routing/polymorphic_routes.rb:200:in `polymorphic_method'
  actionpack (4.2.5.2) lib/action_dispatch/routing/polymorphic_routes.rb:114:in `polymorphic_url'
  actionpack (4.2.5.2) lib/action_dispatch/routing/url_for.rb:164:in `url_for'
  actionpack (4.2.5.2) lib/action_controller/metal/redirecting.rb:95:in `_compute_redirect_to_location'
  turbolinks (2.5.3) lib/turbolinks/xhr_headers.rb:21:in `_compute_redirect_to_location'
  actionpack (4.2.5.2) lib/action_controller/metal/redirecting.rb:75:in `redirect_to'
  actionpack (4.2.5.2) lib/action_controller/metal/flash.rb:57:in `redirect_to'
  actionpack (4.2.5.2) lib/action_controller/metal/instrumentation.rb:64:in `block in redirect_to'
  activesupport (4.2.5.2) lib/active_support/notifications.rb:164:in `block in instrument'
  activesupport (4.2.5.2) lib/active_support/notifications/instrumenter.rb:20:in `instrument'
  activesupport (4.2.5.2) lib/active_support/notifications.rb:164:in `instrument'
  actionpack (4.2.5.2) lib/action_controller/metal/instrumentation.rb:63:in `redirect_to'
  responders (2.1.2) lib/action_controller/responder.rb:145:in `redirect_to'
  responders (2.1.2) lib/action_controller/responder.rb:205:in `navigation_behavior'
  responders (2.1.2) lib/action_controller/responder.rb:172:in `rescue in to_html'
  responders (2.1.2) lib/action_controller/responder.rb:170:in `to_html'
  responders (2.1.2) lib/action_controller/responder.rb:163:in `respond'
  responders (2.1.2) lib/action_controller/responder.rb:156:in `call'
  responders (2.1.2) lib/action_controller/respond_with.rb:205:in `respond_with'
  devise (3.5.6) app/controllers/devise/passwords_controller.rb:17:in `create'


这是我的代码:

app / views / passwords / new.html.erb

<div class="row row-centered">
  <div class="col-md-5 col-centered">
    <div class="panel panel-with-header__container col-min col-max">
        <h3 class="panel-header panel-header-gray forgot_password_container">
          Reset Password
        </h3>

        <div class="panel-body">

            <p>
                Enter the email address associated with your account, and we’ll email you a link to reset your password.
            </p>

            <%= form_for(resource, as: resource_name, url: password_path(resource_name), html: { method: :post }, remote: true) do |f| %>
              <%= devise_error_messages! %>

              <div class="field form-group">
                <%= f.email_field :email, autofocus: true, class: "form-control decorative-input inspectletIgnore", placeholder: "Email address", required: "required" %>
              </div>

              <hr/>

              <div class="actions">
                <%= f.submit "Send Reset Link", class: "btn btn-primary" %>
              </div>
            <% end %>
        </div>


    </div>
  </div>
</div>


app / controllers / passwords_controller.rb

class PasswordsController < Devise::PasswordsController
    respond_to :js

    def create

        self.resource = resource_class.send_reset_password_instructions(resource_params)

        respond_to do |format|

          @message = ""
          if successfully_sent?(resource)
            @message = "success"
          else
            @message = "error"
          end

          format.html
          format.js
        end

    end

    def after_sending_reset_password_instructions_path_for(resource_params)
        root_path
    end

end


app / views / passwords / create.js.rb

<% if @message == "success" %>
    alert('Success!');
<% else %>
    alert('Something wrong!');
<% end %>

最佳答案

通过将devise.rb中的导航格式更新为:

config.navigational_formats = ['*/*', :html, :js]

10-02 16:41