devise默认已经有较好的ajax支持了,只需要简单的配置一下即可使用ajax登录/注册。
1. 修改config/application.rb加下以下配置,启用json输出。
# devise respond_to json config.to_prepare do DeviseController.respond_to :html, :json end
2. 修改form标签,添加data-remote, data-type,设置id
<%= form_for(resource, :as => resource_name, :url => session_path(resource_name), :remote => true, html: {id: 'ajax_user_signin', data: {type: :json}}) do |f| %>
3. 设置ajax头
$.ajaxSetup({ beforeSend: function(xhr){ var token; token = $('meta[name="csrf-token"]').attr('content'); if (token) { xhr.setRequestHeader('X-CSRF-Token', token); } } });
4. js处理代码
$('#ajax_user_signin').on('ajax:complete', function(e, xhr, type){ if (type === 'success') { location.href = '/'; } else { try { alert(xhr.responseJSON.error); // this.reset(); } catch (e$) { e = e$; } } });
关于注册:
注册的方法和登录基本相同,给form_for添加data-remote、data-type和id等属性。需要注意的是注册返回的表单验证是多项错误,使用xhr.resonseJSON.errors获取错误集合。
2022年8月29日 18:32
UP Board 10th Previous Question Paper 2023 are very important for the Preparation of Public Exam of UPMSP for the Students. 10th Students of can easily get their Latest and most valuable Mock Test Paper from this website. UP Board Question Paper Students only have to Download the UP Board 10th Mock Test Paper 2023 for all the important subjects are available Including the English, Hindi, Mathematics, Science, Social Science etc. UP Board 10th Model Paper 2023, UP 10th High School Question Paper 2023 Blueprint, UP Board 10th English, Hindi, Mathematics, Science, Social Science Exam Important Question Paper 2023 Download.
2023年1月31日 14:38
Fourth Generation BSNL 4G SIM upgradation is now live with simple SMS codes, Find the new technology updates readily available for you to get enhance your technical knowledge with evergreen how-to guides along with new latest updates of Bharat Sanchar Nigam Limited. BSNL BSNL SIM card in this new season along with the existing offers of Bharat Sanchar Nigam Limited, where the operator introduces, free upgradation offer to customers with 4G replacement SIM those who are using 2G or 3G SIMS.
2023年8月04日 19:55
Kendriya Vidyalaya Sangathan Provides the 9th Class Book in both the offline and Online Pdf format at official website, Primary and High School Students can either Download the PDF for each subject separately or can Download a Combined PDF in the zip file format.KVS Textbooks 2024 are one of the Perfect study Materials you can Choose for Efficient Preparation ahead, KVS Books are Specifically KVS 9th Class Textbook 2024 Focused on the Syllabus which Suits All Indian Education Boards, Students Download KVS Books 2024 for Online our Web portal Provide Subject Wise and Medium Wise Pdf Format File Complete KVS Curriculum Complete Text Books 2024