用户子类的单独注册过程

时间:2012-12-10 14:42:24

标签: ruby-on-rails devise

我正在尝试为我的用户子类 - 成员和合作伙伴设置2个稍微不同的注册过程。我想这样做:

/ users / sign_up将用户注册为会员(因此我在其中有一个隐藏字段,其值为'Member'),它就像魅力一样。

但我也想要:

/ users / partner / sign_up提供稍微不同的表单,将其归为“合作伙伴”。

我特别希望通过2个单独的网址实现此目的,因此我可以向这些不同类型的用户发送不同的链接以进行注册。

我正在使用Devise作为我的身份验证系统。

我很确定我应该生成一个单独的控制器,比如partner_registrations_controller,并让它继承设计,但我不知道应该在控制器中输入什么代码。

我还认为我需要在views / users文件夹中创建一个新文件夹'partner_registrations',我将在其中使用特定的'new.html.erb'表单。

我终于知道我需要对这些路线做点什么,比如:

  devise_for :users, :controllers => { :registrations => :registrations } do
      get 'users/partner/sign_up', to: 'devise/registrations#new'
  end

我已经在github上阅读了这个wiki页面:https://github.com/plataformatec/devise/wiki/How-To:-Customize-routes-to-user-registration-pages但我不是更明智的。

非常感谢任何帮助。

1 个答案:

答案 0 :(得分:0)

我弄清楚了,所以我想发布答案,以防万一其他人认为将来有用:

路由文件:

  devise_scope :user do
    get 'sign_up', to: 'members#new', controller: {registrations: "members"}
    get 'partners/sign_up', to: 'content_partners#new', controller: {registrations: "content_partners"}
  end
  devise_for :users, controllers: {registrations: :registrations}

我为每个子类创建了2个独立的控制器,基本上与设计给出的新操作和创建操作相同:

class MembersController < Devise::RegistrationsController
  def new
    resource = build_resource({})
    respond_with resource
  end

  # POST /resource
  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)
        respond_with resource, :location => after_sign_up_path_for(resource)
      else
        set_flash_message :notice, :"signed_up_but_#{resource.inactive_message}" if is_navigational_format?
        expire_session_data_after_sign_in!
        respond_with resource, :location => after_inactive_sign_up_path_for(resource)
      end
    else
      clean_up_passwords resource
      respond_with resource
    end
  end
end

我刚刚在控制器中替换了另一个子类的'ContentPartners'的'Members'。

然后我在Views / Users文件夹中创建了2个新文件夹 - 注意得到我的问题,他们也必须复数 - 所以/ members和/ content_partners。然后在每个文件夹中我创建了一个唯一的“new.html.erb”文件。

就是这样。