2017-06-18 41 views
0

我允許用戶使用Facebook註冊並使用omniauth-facebook and Devise登錄。除非驗證失敗,否則它工作得很好。在這種情況下,Devise的新註冊頁面會加載,但不會出現任何錯誤消息。Rails驗證不會在OmniAuth和Devise中顯示錯誤視圖

如果我嘗試通過在表單中​​輸入字段進行註冊,則顯示錯誤 - 僅當用戶試圖使用Facebook註冊時纔會顯示錯誤。

我該如何獲得模型驗證錯誤持續顯示在新的註冊表單上?

user.rb

validates :username, :email, uniqueness: true 

def self.from_omniauth(auth) 
    where(provider: auth.provider, uid: auth.uid).first_or_create do |user| 
    user.email = auth.info.email 
    user.password = Devise.friendly_token[0,20] 
    user.username = auth.info.name.downcase.gsub(" ", "") 
    user.ensure_username_uniqueness 
    end 
end 

def ensure_username_uniqueness 
    self.username ||= self.email.split("@").first 
    num = 2 
    until(User.find_by(username: self.username).nil?) 
    self.username = "#{username_part}#{num}" 
    num += 1 
    end 
end 

omniauth_callbacks_controller.rb

def facebook 
    @user = User.from_omniauth(request.env["omniauth.auth"]) 

    if @user.persisted? 
    sign_in_and_redirect @user, :event => :authentication 
    set_flash_message(:notice, :success, :kind => "Facebook") if is_navigational_format? 
    else 
    session["devise.facebook_data"] = request.env["omniauth.auth"] 
    redirect_to new_user_registration_url 
    end 
end 

色器件/註冊/ new.html.erb

<%= simple_form_for(resource, as: resource_name, url: registration_path(resource_name)) do |f| %> 
    <% if object.errors.any? %> 
    <div class="alert alert-danger" role="alert"> 
     These errors occurred: 
     <ul> 
     <% object.errors.full_messages.each do |msg| %> 
      <li><%= msg %></li> 
     <% end %> 
     </ul> 
    </div> 
    <% end %> 

    <div class="form-inputs"> 
    <%= f.input :username, required: true, autofocus: true %> 
    <%= f.input :email, required: true, autofocus: true %> 
    <%= f.input :password, required: true %> 
    <%= f.input :password_confirmation, required: true %> 
    </div> 

    <%= f.button :submit, "Sign up", :class=>"btn-info" %> 
<% end %> 

回答

0

的問題是,我使用的是不是重定向渲染的一部分。

後,我改變了:

redirect_to new_user_registration_url 

到:

render :template => "devise/registrations/new" 

錯誤消息顯示。

相關問題