我有一个用户注册的ajax表单。我尝试登录未经确认的用户时遇到问题。如果我输入有效的用户名和密码,如果我为用户输入了错误的密码,我就进入创建操作并从invalid_login_attempt方法返回良好响应。它永远无法从会话控制器创建操作。
会话控制器:
class SessionsController < Devise::SessionsController
def create
debugger
resource = Bbp::User.find_for_database_authentication(email: params[:user][:email])
return invalid_login_attempt unless resource
return invalid_login_attempt unless resource.valid_password?(params[:user][:password])
return confirmation_needed if resource.confirmed_at.nil?
if resource.valid_password?(params[:user][:password])
sign_in :user, resource
return render nothing: true
end
invalid_login_attempt
end
protected
def invalid_login_attempt
render json: "Wrong username or password", status: 401
end
def confirmation_needed
render json: "You need to confirm your email before you can continue", status: 401
end
end
我的表格是:
<h2>Sign in</h2>
<div class="message-success-field user-sign-in-row"></div>
<%= form_for(resource, as: resource_name, url: '/admin.json',html: {id: 'new_user_session'}, remote: true ) do |f| %>
<div class="form-group">
<%= f.email_field :email, class: "form-control", placeholder: "Email", autofocus: true %>
</div><!-- form-group -->
<div class="form-group">
<%= f.password_field :password, class: "form-control", placeholder: "Password #{'(' + @minimum_password_length.to_s + ' characters minimum)' if @minimum_password_length}", autocomplete: "off" %>
</div><!-- form-group -->
<% if devise_mapping.recoverable? %>
<small><%= link_to "Forgot your password?", new_password_path(resource_name) %></small><br />
<% end %>
<% if devise_mapping.rememberable? %>
<div class="field">
<%= f.check_box :remember_me %>
<%= f.label :remember_me %>
</div>
<% end %>
<%= f.button "Sign in", class: "form-control btn btn-success" %>
<p>Or connect via Facebook</p>
<%= link_to "Sign in with facebook", "/auth/facebook", class: "form-control btn btn-primary" %>
<% end %>
<script>
$(document).ready(function() {
$('#new_user_session')
.bind('ajax:success', function(response, evt, data, status, xhr) {
$('div.user-sign-in-row').empty()
$('div.user-sign-in-row').append("You are successfully logged in")
})
.bind("ajax:error", function(evt, xhr, status, error) {
$('div.user-sign-in-row').empty()
$('div.user-sign-in-row').append(xhr.responseText)
});
});
</script>
我认为这是因为在创建之前的某个地方,设计检查用户是否可以确认,但我无法找到它的位置。我还设置会话控制器响应html和json。
有什么问题?