设计 - 登录使用Ajax [英] Devise - Sign In with Ajax

查看:173
本文介绍了设计 - 登录使用Ajax的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在这里任何可能性修改色器件的 SessionsController 作为阿贾克斯的沟通?

修改

我找到了解决办法,并张贴到答案,谢谢

解决方案

我是找到任何答案,但我还没有发现任何工作或实际的,所以我花了一些时间,解决了它在我自己的。这里是我的答案。

1。生成设计的控制器,所以我们可以对其进行修改

 轨道摹色器件:控制器
 

现在,我们已经得到了所有的控制器在我们的应用程序/控制器/ [产品型号] 文件夹

2。编辑routes.rb中

我们必须告诉制订使用修改后的SessionsController

  

的config / routes.rb中

和补充,当然这个code,更改:用户以你的色器件模型

  devise_for:用户,控制器:{
  会议:用户/会话
}
 

3。修改sessions_controller.rb

查找创建方法

  

将其更改为

 高清创建
  资源= User.find_for_database_authentication(电子邮件:PARAMS [:用户] [:邮箱])
  返回invalid_login_attempt除非资源

  如果resource.valid_password?(PARAMS [:用户] [:密码])
    sign_in:用户资源
    返回渲染什么:真
  结束

  invalid_login_attempt
 结束
 

  

和后创建新的方法保护

 高清invalid_login_attempt
  set_flash_message(:警告,:无效)
  渲染JSON:闪光[:警惕],状态:401
结束
 

4。 devise.rb

  

添加这些行到配置/初始化/ devise.rb

  config.http_authenticatable_on_xhr = FALSE
config.navigational_formats =* / *:HTML,:JSON]
 

5。无效的电子邮件或密码信息

  

插入新邮件到配置/区域设置/ devise.en.yml

 无效:无效的电子邮件或密码。
 

6。查看

  =的form_for资源,网址:session_path(:用户),遥控器:真做| F |
  = f.text_field:电子邮件
  = f.password_field:密码
  = f.label:remember_me做
    记住我
    = f.check_box:remember_me
  = f.submit值:'登录'

:JavaScript的
  $(文件)。就绪(函数(){
    //表单ID
    $('#new_user)
    .bind('阿贾克斯:成功',功能(EVT,数据,身份,XHR){
      //函数调用的状态:(为前)200
      的console.log(成功);
    })
    .bind(AJAX:错误,功能(EVT,XHR,状态,错误){
      //函数调用的状态:(为前)401或500
      执行console.log(xhr.responseText);
    });
  });
 

  

重要的事情远程:真正的

     

我之所以使用状态200或401 {不同状态:真正的}或   类似的东西是较少的数据的大小,所以其更快和更干净。

说明

在登录时,你在PARAMS这些数据

 的行动:创造
承诺:登录
控制器:用户/会话
用户: {
  电子邮件:test@test.cz
  密码:123
  remember_me:0
}
UTF8:✓
 

签约之前,你需要授权用户。

 资源= User.find_for_database_authentication(电子邮件:PARAMS [:用户] [:邮箱])
 

User.find_for_database_authentication

如果用户发现,资源会装满像

  created_at:2015-05-29T12:48:04.000Z
电子邮件:test@test.cz
ID:1
的updated_at:2015-06-13T19:56:54.000Z
 

否则会

  

如果用户的授权,我们要验证密码

 如果resource.valid_password?(PARAMS [:用户] [:密码])
 

最后登录

  sign_in:用户资源
 

来源

<一个href="https://github.com/plataformatec/devise/blob/master/app/controllers/devise/sessions_controller.rb"相对=nofollow> SessionsController

帮我 <一href="http://stackoverflow.com/questions/22758288/ruby-devise-sessionscontroller-create-json-getting-nameerror-undefined-bui">Andreas Lyngstad

Is here any possibility to modify devise SessionsController for ajax communication?

Edit

I found the solution, and posted it into answers, thanks

解决方案

I was finding for any answer but I haven't found anything working or actual so I spend some time and solved it at my own. Here's my answer.

1. Generate Devise controllers so we can modify it

rails g devise:controller

Now we've got all controllers in our app/controllers/[model] folder

2. Edit routes.rb

We have to tell Devise to use our modified SessionsController

config/routes.rb

And add this code, of course change :users to your devise model

devise_for :users, controllers: {
  sessions: 'users/sessions'
}

3. Modify sessions_controller.rb

Find for create method

Change it to

def create
  resource = User.find_for_database_authentication(email: params[:user][:email])
  return invalid_login_attempt unless resource

  if resource.valid_password?(params[:user][:password])
    sign_in :user, resource
    return render nothing: true
  end

  invalid_login_attempt
 end

And create new method after protected

def invalid_login_attempt
  set_flash_message(:alert, :invalid)
  render json: flash[:alert], status: 401
end

4. devise.rb

Add those lines into config/initializers/devise.rb

config.http_authenticatable_on_xhr = false
config.navigational_formats = ["*/*", :html, :json]

5. Invalid email or password message

Insert new message into config/locales/devise.en.yml under the sessions

invalid: "Invalid email or password."

6. View

= form_for resource, url: session_path(:user), remote: true do |f|
  = f.text_field :email
  = f.password_field :password
  = f.label :remember_me do
    Remember me
    = f.check_box :remember_me
  = f.submit value: 'Sign in'

:javascript
  $(document).ready(function() {
    //form id
    $('#new_user')
    .bind('ajax:success', function(evt, data, status, xhr) {
      //function called on status: 200 (for ex.)
      console.log('success');
    })
    .bind("ajax:error", function(evt, xhr, status, error) {
      //function called on status: 401 or 500 (for ex.)
      console.log(xhr.responseText);
    });
  });

Important thing remote: true

The reason why I use status 200 or 401 unlike {status: 'true'} or something like that is less data size, so its much faster and cleaner.

Explanation

On signing in, you get these data in params

action: "create"
commit: "Sign in"
controller: "users/sessions"
user: {
  email: "test@test.cz"
  password: "123"
  remember_me: "0"
}
utf8: "✓"

Before signing, you need to authorize user.

resource = User.find_for_database_authentication(email: params[:user][:email])

User.find_for_database_authentication

If user's found, resource would be filled with something like

created_at: "2015-05-29T12:48:04.000Z"
email: "test@test.cz"
id: 1
updated_at: "2015-06-13T19:56:54.000Z"

Otherwise would be

null

If user's authorized, we are going to validate password

if resource.valid_password?(params[:user][:password])

And finally sign in

sign_in :user, resource

Sources

SessionsController

Helped me Andreas Lyngstad

这篇关于设计 - 登录使用Ajax的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆