jac*_*992 3 ruby ruby-on-rails devise twitter-bootstrap
我正在使用Ruby on Rails和bootstrap开发一个应用程序.我正在使用Devise gem进行身份验证系统.我生成了设计视图,但我不喜欢使用页面/视图进行登录/注册.相反,我得到了一个我正在使用的弹出模式/形式.现在我的登录表单如下所示:
<div class="form loginBox">
<%= form_for(resource, as: resource_name, url: session_path(resource_name)) do |f| %>
<%= devise_error_messages! %>
<%= f.email_field :email, :class => "form-control", :placeholder => "Email" %>
<%= f.password_field :password, :class => "form-control", :placeholder => "Password", autocomplete: "off" %>
<% if devise_mapping.rememberable? -%>
<%= f.check_box :remember_me, :style => "margin-top:20px;margin-bottom:20px;" %>
<%= f.label :remember_me, :style => "margin-top:20px;margin-bottom:20px;" %>
<% end -%>
<%= f.submit "Sign In", :class => "btn btn-default btn-login" %>
<% end %>
</div>
Run Code Online (Sandbox Code Playgroud)
我把它放在应用程序帮助器中:
module ApplicationHelper
def resource_name
:user
end
def resource
@resource ||= User.new
end
def devise_mapping
@devise_mapping ||= Devise.mappings[:user]
end
end
Run Code Online (Sandbox Code Playgroud)
当我按下登录按钮(提交按钮)而没有填写字段时,我被重定向到/ users/sign_in页面,在那里我得到了设计错误消息.我的注册表格也是如此.
我怎样才能防止这种情况发生?对于原始设计视图我应该怎么做才能显示它们?我这样做是错误的吗?
我对Ruby和Rails很新,我很高兴我至少使用我的弹出窗口进行身份验证登录/注册过程.只是因为我得到了上面提到的问题,我不确定我是否正确地做到了这一点.
非常感谢提前!/雅各布
您需要自定义会话控制器和一些额外配置.
第1步:启用设计以使用JSON进行响应
# config/initializers/devise.rb
config.http_authenticatable_on_xhr = false
config.navigational_formats = ["*/*", :html, :json]
Run Code Online (Sandbox Code Playgroud)
第2步:添加映射(我可以看到你已经这样做了)
# app/helpers/application_helper.rb
module ApplicationHelper
def resource_name
:user
end
def resource
@resource ||= User.new
end
def devise_mapping
@devise_mapping ||= Devise.mappings[:user]
end
end
Run Code Online (Sandbox Code Playgroud)
第3步:添加自定义控制器
# app/controllers/sessions_controller.rb
class SessionsController
def create
resource = warden.authenticate!(:scope => resource_name, :recall => '#{controller_path}#failure')
sign_in_and_redirect(resource_name, resource)
end
def sign_in_and_redirect(resource_or_scope, resource=nil)
scope = Devise::Mapping.find_scope!(resource_or_scope)
resource ||= resource_or_scope
sign_in(scope, resource) unless warden.user(scope) == resource
return render :json => {:success => true}
end
def failure
return render :json => {:success => false, :errors => ["Login failed."]}
end
end
Run Code Online (Sandbox Code Playgroud)
这里控制器的特殊之处在于我们将所有内容都归还 :json
第3步:覆盖控制器:
# config/routes.rb
devise_for :users, :controllers => {sessions: 'sessions'}
Run Code Online (Sandbox Code Playgroud)
现在设计使用自定义会话控制器.
第4步:添加自定义表单(使用:remote => true)
%h2 Sign in
<%= devise_error_messages! %>
<%= form_for(resource, :as => resource_name, :url => session_path(resource_name) ,:html => {:id => "login-box"},:format => :json, :remote => true ) do |f| %>
<%= f.password_field :password, :class => "form-control", :placeholder => "Password", autocomplete: "off" %>
<% if devise_mapping.rememberable? -%>
<%= f.check_box :remember_me, :style => "margin-top:20px;margin-bottom:20px;" %>
<%= f.label :remember_me, :style => "margin-top:20px;margin-bottom:20px;" %>
<% end -%>
<%= f.submit "Sign In", :class => "btn btn-default btn-login" %>
<% end %>
</div>
Run Code Online (Sandbox Code Playgroud)
第5步:添加javascript
$("form#login-box").bind "ajax:success", (e, data, status, xhr) ->
if data.success
//javascript that executes if everything goes o.k.
$('#sign_in').modal('hide')
$('#sign_in_button').hide()
$('#submit_comment').slideToggle(1000, "easeOutBack" )
else
alert('failure!')
Run Code Online (Sandbox Code Playgroud)
这应该工作
| 归档时间: |
|
| 查看次数: |
4714 次 |
| 最近记录: |