1

我正在为我的 Rails 应用程序使用设计,我希望通过 Ajax 处理注册。目前我正在跟进本教程。到目前为止,用户注册工作(用户数据通过 Ajax 插入数据库),但用户是注册后不会自动登录。我如何做到这一点?有 这个教程显示了 Ajax 登录步骤,但我不确定我是否需要它,如果是的话如何?这是我的代码

注册控制器.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 %>
4

0 回答 0