Railsアプリにdeviseを使用していますが、Ajaxを介してサインアップを処理したい.現在、このチュートリアルをフォローアップしています.これまでのところ、ユーザー登録は機能しています(ユーザーデータはAjaxを介してデータベースに挿入されます)が、ユーザーは登録後に自動的にサインインされません。どうすればこれを達成できますか? Ajax サインイン手順を示すこのチュートリアルがありますが、必要かどうかはわかりません。必要な場合はどうすればよいですか? これが私のコードです
registrations_controller.rb
class RegistrationsController < Devise::RegistrationsController
def create
build_resource
if resource.save
if resource.active_for_authentication?
set_flash_message :notice, :signed_up if is_navigational_format?
sign_up(resource_name, resource)
return render :json => {:success => true}
else
set_flash_message :notice, :"signed_up_but_#{resource.inactive_message}" if is_navigational_format?
expire_session_data_after_sign_in!
return render :json => {:success => true}
end
else
clean_up_passwords resource
return render :json => {:success => false}
end
end
# Signs in a user on sign up. You can overwrite this method in your own
# RegistrationsController.
def sign_up(resource_name, resource)
sign_in(resource_name, resource)
end
end
ルート
devise_for :users
devise_for :users , :controllers => {registrations: 'registrations'}
ヘルパー
module MyappHelper
def resource_name
:user
end
def resource
@resource ||= User.new
end
def devise_mapping
@devise_mapping ||= Devise.mappings[:user]
end
end
形
<%= form_for(resource,:as=>resource_name,:url => registration_path(resource_name)) do |f| %>
<%= devise_error_messages! %>
<%= f.label :Username %>
<%= f.text_field :username %></p>
<%= f.label :email %>
<%= f.text_field :email %>
<%= f.label :password %>
<%= f.password_field :password %>
<%= f.label :password_confirmation %>
<%= f.password_field :password_confirmation %>
<%= f.submit "Sign up" %>
<% end %>