【问题标题】:ActionController::UrlGenerationError in Devise::SessionsController#create / Trying to Create 2 Signup Access with deviseActionController::UrlGenerationError in Devise::SessionsController#create / Trying to Create 2 Signup Access with devise
【发布时间】:2014-08-07 14:51:42
【问题描述】:

我是 Rails 新手,如果以下问题可能很简单,我深表歉意,但我们将不胜感激。

我正在使用 GEM 设计。我为用户(代表交易者)和房主设置了 2 个身份验证系统(使用设计)

当用户登录时,我希望他们被定向到 users/show.html.erb 页面,当房主登录时,我希望他们被定向到 homeowners/show.html.erb

我收到的错误消息:

  1. 我在 用户(商人)和房主的 application_controller.rb

  2. 当我为 Homeowner 删除设计 after_sign_in_path_for 方法时 并将编码设计 after_sign_in_path_for 方法留给用户 (商人).....当我以用户(商人)身份登录时,我没有收到任何错误 我被定向到个人资料页面(users/show.html.erb)...(副 反之亦然)。

  3. 但是当我离开用户(商人)和房主的 after_sign_in_path_for 方法并以用户(商人)或房主身份登录时,我收到以下错误消息

我正在尝试为用户(商人)和房主创建两个登录访问权限

Started POST "/users/sign_in" for 127.0.0.1 at 2014-08-07 14:59:36 +0100
Processing by Devise::SessionsController#create as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"dI4uxvSE2tXlUeNlBCSLTbuvgRiUhdE5V1NfB8Y+htY=", "user"=>{"email"=>"richill@gmail.com", "password"=>"[FILTERED]", "remember_me"=>"0"}, "commit"=>"Tradesman login"}
  User Load (0.2ms)  SELECT  "users".* FROM "users"  WHERE "users"."email" = 'richill@gmail.com'  ORDER BY "users"."id" ASC LIMIT 1
   (0.1ms)  begin transaction
  SQL (0.3ms)  UPDATE "users" SET "current_sign_in_at" = ?, "last_sign_in_at" = ?, "sign_in_count" = ?, "updated_at" = ? WHERE "users"."id" = 7  [["current_sign_in_at", "2014-08-07 13:59:36.930831"], ["last_sign_in_at", "2014-08-07 13:57:07.157341"], ["sign_in_count", 7], ["updated_at", "2014-08-07 13:59:36.931472"]]
   (1.4ms)  commit transaction
Completed 500 Internal Server Error in 73ms

ActionController::UrlGenerationError (No route matches {:action=>"show", :controller=>"homeowners", :id=>nil} missing required keys: [:id]):
  app/controllers/application_controller.rb:12:in `after_sign_in_path_for'

views/homeowners/sessions/new.html.erb(这代表房主登录访问)

<h2>Are you a Homeowner?</h2>

<%= simple_form_for(resource, as: resource_name, url: session_path(resource_name)) do |f| %>
  <div class="form-inputs">
    <%= f.input :email, required: false, autofocus: true %>
    <%= f.input :password, required: false %>
    <%= f.input :remember_me, as: :boolean if devise_mapping.rememberable? %>
  </div>

  <div class="form-actions">
    <%= f.button :submit, "Homeowner login" %>
  </div>
<% end %>
<%= render "homeowners/shared/links" %>

views/devise/sessions/new.html.erb(这代表交易者登录访问)

<%= form_for(resource, as: resource_name, url: session_path(resource_name)) do |f| %>
  <h1>Are you a Tradesman?</h1>
  <div><%= f.label :email %><br />
  <%= f.email_field :email, autofocus: true %></div>

  <div><%= f.label :password %><br />
    <%= f.password_field :password, autocomplete: "off" %></div>

  <% if devise_mapping.rememberable? -%>
    <div><%= f.check_box :remember_me %> <%= f.label :remember_me %></div>
  <% end -%>

  <div><%= f.submit "Tradesman login" %></div>
<% end %>

<%= render "devise/shared/links" %></br>

application_controller.rb - 我相信错误与这里有关,但不确定如何解决它当我注释掉为房主设计 after_sign_in_path_for 方法并且我以用户(商人)身份登录时,我没有收到任何错误我被定向到个人资料页面(users/show.html.erb)。当我注释掉 User(Tradesman) 的 devise after_sign_in_path_for 方法并且我以房主身份登录时,我没有收到任何错误,并且我被定向到个人资料页面 (homeowners/show.html.erb)。当我离开 after_sign_in_path_for 方法并尝试以用户(商人)或房主身份登录时,我收到上述错误

class ApplicationController < ActionController::Base
  # Prevent CSRF attacks by raising an exception.
  # For APIs, you may want to use :null_session instead.
  protect_from_forgery with: :exception

  protected
  def after_sign_in_path_for(resource)
    user_path(current_user)            #<--- For the Tradesman
  end

  def after_sign_in_path_for(resource)
    homeowner_path(current_homeowner)  #<--- For the Homeowner
  end
end

registration_controller.rb

class RegistrationsController < Devise::RegistrationsController
    before_filter :configure_permitted_parameters, if: :devise_controller?

    protected
    def after_sign_up_path_for(resource)
    '/user_steps'                          
    end

    def configure_permitted_parameters
    devise_parameter_sanitizer.for(:sign_up) { |u| u.permit(:trade_id, :location, :email, :password, :password_confirmation, :trade_id, :location, :title_id, :first_name, :last_name, :company_name, :company_number, :vat_number, :mobile, :business_role_id, :business_type_id, :employee_number_id, :card_name, :card_number, :card_date, :card_code, :role) }
    end
end

routes.rb

Rails.application.routes.draw do
  devise_for :homeowners
  devise_for :users, :controllers => { :registrations => "registrations" }
  resources :employee_numbers
  resources :business_types
  resources :business_roles
  resources :titles
  resources :form_signuptmas
  resources :homeowners
  resources :users
  get '/about', to: 'static_pages#about'
  get '/find', to: 'static_pages#find'
  get '/ask', to: 'static_pages#ask'
  get '/contact', to: 'static_pages#contact'
  get 'home/index'
  resources :user_steps
  resources :jobs
  resources :trades
  root to: 'home#index', as: 'home'
end

【问题讨论】:

    标签: ruby-on-rails ruby ruby-on-rails-4 devise


    【解决方案1】:

    您不能定义 2 个具有相同名称的方法。第二种方法将覆盖第一种。

    如果您查看日志,您可以看到传递了一个参数来判断用户是否是商人

    "commit"=>"Tradesman login"
    

    所以你的方法定义看起来像

    def after_sign_in_path_for(resource)
      if params[:commit] == 'Tradesman login'
        user_path(current_user)
      else
        homeowner_path(current_homeowner)
      end
    end
    

    【讨论】:

      猜你喜欢
      • 2019-07-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多