【问题标题】:Devise after_sign_up_for path not working设计 after_sign_up_for 路径不起作用
【发布时间】:2016-10-27 09:22:00
【问题描述】:

我阅读了所有关于此的主题,但我仍然无法弄清楚哪里出了问题。我已经阅读了所有设计实施指南并遵循了它们,但是在新用户注册后,重定向会转到用户控制器,特别是 users#show。

对新注册的初始调用是通过我的注册控制器进行的。这已经得到验证。但是在注册之后它不会再次通过注册控制器。它通过用户控制器。我在新的注册控制器中中断了两个覆盖方法,但它们从未成功。

/routes.rb

Rails.application.routes.draw do
  root to: 'app#index'

  devise_for :users, controllers: { registrations: "registrations" }

  resources :users do
    member do
      put 'follow', to: 'users#follow'
      put 'unfollow', to: 'users#unfollow'
    end
  end

end

registrations_controller.rb

class RegistrationsController < Devise::RegistrationsController
protected
  #call for new registration does pass through here
  #after new registration does not pass through here


  def after_inactive_sign_up_path_for(resource)
    #never gets here after new registration
    other_path(resource)
  end

  def after_sign_up_path_for(resource)
    #never gets here after new registration
    other_path(resource)
  end

end

新的注册表:

  <form action="index.html">

    <%= form_for(resource, as: resource_name, url: registration_path(resource_name)) do |f| %>
      <%= devise_error_messages! %>

    <div class="form-group">
      <%= f.email_field :email, autofocus: true, placeholder: "Email", class: "form-control rounded input-lg text-center no-border" %>
    </div>

    <div class="form-group">
      <% if @minimum_password_length %>
      <em>(<%= @minimum_password_length %> characters minimum)</em>
      <% end %><br />
      <%= f.password_field :password, autocomplete: "off", placeholder: "Password", class: "form-control rounded input-lg text-center no-border" %>

    </div>

    <div class="form-group">
      <%= f.password_field :password_confirmation, autocomplete: "off", placeholder: "Confirm Password", class: "form-control rounded input-lg text-center no-border"  %>
    </div>

    <div class="checkbox i-checks m-b">
      <label class="m-l">
        <input type="checkbox" checked=""><i></i> Agree the <a href="#">terms and policy</a>
      </label>
    </div>

    <button type="submit" class="btn btn-lg btn-warning lt b-white b-2x btn-block btn-rounded"><i class="icon-arrow-right pull-right"></i><span class="m-r-n-lg">Sign up</span></button>
    <% end %>

    <div class="text-center m-t m-b"><small><%= link_to "Already have an account?", new_session_path(resource_name) %></small></div>

控制台:

Started GET "/users/index.html?utf8=%E2%9C%93&authenticity_token=KlAk4ctYpw%2F2eHGqZ0OsAymf5BxUPxeBiFzeOatn2JDUQJdPk8fGoB%2BmuN4oeN3KviQdVwN%2FxMfnjH4zIVooOg%3D%3D&user%5Bemail%5D=asdf%40asdf.com&user
%5Bpassword%5D=[FILTERED]&user%5Bpassword_confirmation%5D=[FILTERED]" for ::1 at 2016-10-25 22:10:37 -0400
Processing by UsersController#show as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"KlAk4ctYpw/2eHGqZ0OsAymf5BxUPxeBiFzeOatn2JDUQJdPk8fGoB+muN4oeN3KviQdVwN/xMfnjH4zIVooOg==", "user"=>{"email"=>"asdf@asdf.com", "password"=>"[FILTERED]
", "password_confirmation"=>"[FILTERED]"}, "id"=>"index"}
  User Load (0.3ms)  SELECT  "users".* FROM "users" WHERE "users"."id" = $1 LIMIT $2  [["id", 0], ["LIMIT", 1]]

Completed 404 Not Found in 3ms (ActiveRecord: 0.3ms)



ActiveRecord::RecordNotFound (Couldn't find User with 'id'=index):

app/controllers/users_controller.rb:6:in `show'
  Rendering /Users/Joe/.rbenv/versions/2.3.0/lib/ruby/gems/2.3.0/gems/actionpack-5.0.0.1/lib/action_dispatch/middleware/templates/rescues/diagnostics.html.erb within rescues/layout
  Rendering /Users/Joe/.rbenv/versions/2.3.0/lib/ruby/gems/2.3.0/gems/actionpack-5.0.0.1/lib/action_dispatch/middleware/templates/rescues/_source.html.erb
  Rendered /Users/Joe/.rbenv/versions/2.3.0/lib/ruby/gems/2.3.0/gems/actionpack-5.0.0.1/lib/action_dispatch/middleware/templates/rescues/_source.html.erb (3.7ms)
  Rendering /Users/Joe/.rbenv/versions/2.3.0/lib/ruby/gems/2.3.0/gems/actionpack-5.0.0.1/lib/action_dispatch/middleware/templates/rescues/_trace.html.erb
  Rendered /Users/Joe/.rbenv/versions/2.3.0/lib/ruby/gems/2.3.0/gems/actionpack-5.0.0.1/lib/action_dispatch/middleware/templates/rescues/_trace.html.erb (1.8ms)
  Rendering /Users/Joe/.rbenv/versions/2.3.0/lib/ruby/gems/2.3.0/gems/actionpack-5.0.0.1/lib/action_dispatch/middleware/templates/rescues/_request_and_response.html.erb
  Rendered /Users/Joe/.rbenv/versions/2.3.0/lib/ruby/gems/2.3.0/gems/actionpack-5.0.0.1/lib/action_dispatch/middleware/templates/rescues/_request_and_response.html.erb (2.0ms)
  Rendered /Users/Joe/.rbenv/versions/2.3.0/lib/ruby/gems/2.3.0/gems/actionpack-5.0.0.1/lib/action_dispatch/middleware/templates/rescues/diagnostics.html.erb within rescues/layout (69.3ms)

请求参数:

{"utf8"=>"✓",
 "authenticity_token"=>"KlAk4ctYpw/2eHGqZ0OsAymf5BxUPxeBiFzeOatn2JDUQJdPk8fGoB+muN4oeN3KviQdVwN/xMfnjH4zIVooOg==",
 "user"=>{"email"=>"asdf@asdf.com", "password"=>"[FILTERED]", "password_confirmation"=>"[FILTERED]"},
 "id"=>"index",
 "format"=>"html"}

【问题讨论】:

  • 粘贴完整的注册控制器。您的注册方法创建错误,表单应重定向到registrations create,但它会向用户显示,您的表单的网址错误。请粘贴表格
  • @Sravan 就是这样!! index.html 有一个额外的前导表单标记。更新了上面的代码以供显示。额外的表单标签是前一段代码遗留下来的。
  • 现在完成了吗?如果没有,您的注册控制器路径是什么

标签: ruby-on-rails devise


【解决方案1】:

如果你想去users#show,正确的路径助手应该是user_path(resource),是你想要的吗?

【讨论】:

    【解决方案2】:

    看起来问题有两个方面。注册控制器正在寻找不正确的路径,但它也需要正确的 id。我认为这里的解决方案是将它传递给user_path(@user.id),而不是在new_user_path(resource) 中将资源作为参数。

    它正在尝试查询 ID 为“Index”但不存在的用户,因此会引发错误。您需要找到新用户的 id 并将其插入到路由中,以便找到该特定用户配置文件。我以@user 为例。您可以在代码中定义该变量或使用您希望的任何变量。希望对您有所帮助!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-07-06
      • 2023-02-21
      • 2018-01-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多