2015-06-27 68 views
0

我需要使用電子郵件和cast_id進行用戶登錄。這是我的代碼:設計 - 通過電子郵件和cast_id登錄

用戶模式

class User < ActiveRecord::Base 

    devise :database_authenticatable, :registerable, :recoverable, 
      :rememberable, :trackable, :validatable, request_keys: [:cast] 

    belongs_to :cast 

    def self.find_for_database_authentication(warden_conditions) 
     where(:email => warden_conditions[:email], :cast => warden_conditions[:cast]).first 
    end 
end 

唱查看

<h2>Log in</h2> 

<%= form_for(resource, as: resource_name, url: session_path(resource_name)) do |f| %> 
    <div class="field"> 
    <%= f.label :email %><br /> 
    <%= f.email_field :email, autofocus: true %> 
    </div> 

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

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

    <div class="actions"> 
    <%= f.submit "Log in" %> 
    <%= f.hidden_field :cast, value: @cast.id.to_s %> 
    </div> 
<% end %> 

<%= render "users/shared/links" %> 

在我加入

config.request_keys = [:cast] 

初始化我跟着這個教程https://github.com/plataformatec/devise/wiki/How-to:-Scope-login-to-subdomain

但這個錯誤是顯示

undefined method `cast' for #<ActionDispatch::Request:0x007f17780feab0> 

def request_values 
    keys = request_keys.respond_to?(:keys) ? request_keys.keys : request_keys 
    values = keys.map { |k| self.request.send(k) } 
    Hash[keys.zip(values)] 
    end 
+0

什麼的色器件的版本? –

回答

0

先試着修改warden_conditions這樣:

def self.find_for_database_authentication(warden_conditions) 
    where(:email => warden_conditions[:email], :cast_id => warden_conditions[:cast]).first 
end 

如果你沒有成功,嘗試另一種方法

用戶.rb

devise :database_authenticatable, :registerable, :recoverable, 
:rememberable, :trackable, :validatable, authentication_keys:{email:true, login:false, cast_id:true} 
def self.find_for_database_authentication(warden_conditions)  
    where(:email => warden_conditions[:email], :cast_id => warden_conditions[:cast_id]).first 
end 

視圖

<div class="actions"> 
    <%= f.submit "Log in" %> 
    <%= f.hidden_field :cast_id, value: @cast.id %> 
</div> 
相關問題