在Devise中创建备用注册操作

时间:2014-09-30 00:01:56

标签: ruby-on-rails ruby-on-rails-4 devise routes

我有一个工作注册和使用Devise& amp; Rails 4.然而,我还创建了一个邀请系统,现有用户可以邀请其他人加入他们的组织。出于这个原因,我想要一个新的页面,其中包含注册和注册表单,以便当他们点击他们的邀请链接(看起来像:/ register_from_invitation / TOKEN_HERE)时,他们将能够注册接受邀请,或者如果他们已有帐户,请登录接受。

所以在我的路线中我有:

devise_for :users, :controllers => {
  :sessions => "sessions",
  :registrations => "registrations",
  :confirmations => "confirmations",
  :passwords => "passwords"
}

devise_scope :user do
  get "register_from_invitation/:token", to: "registrations#new_from_invitation", as: "register_from_invitation"
  post "register_from_invitation/:token", to: "registrations#create_from_invitation", as: "create_registration_from_invitation"
  post "sign_in_from_invitation/:token", to: "sessions#create_from_invitation", as: "sign_in_from_invitation"
end

我将专注于RegistrationsController。除了“普通”newcreate方法之外,它还有另外两个(与原件密切相关)以响应上述路线:

def new_from_invitation
  @invitation = Invitation.where(token: params[:token]).first
  build_resource({})
  self.resource.organisations << @invitation.organisation
  respond_with self.resource
end

def create_from_invitation
  @invitation = Invitation.where(token: params[:token]).first
  build_resource(sign_up_params)

  if resource.save
    resource.organisations << @invitation.organisation
    resource.roles[0].roles << @invitation.role.to_sym
    resource.roles[0].save
    @invitation.destroy
    SignupNotificationMailer.signup_notification_email(resource).deliver
    yield resource if block_given?
    if resource.active_for_authentication?
      set_flash_message :notice, :signed_up if is_flashing_format?
      sign_up(resource_name, resource)
      respond_with resource, :location => '/users/sign_up'
    else
      set_flash_message :notice, :"signed_up_but_#{resource.inactive_message}" if is_flashing_format?
      expire_data_after_sign_in!
      respond_with resource, :location => '/users/sign_up'
    end
  else
    clean_up_passwords resource
    respond_with resource
  end
end

当我在没有正确完成表单的情况下点击注册按钮时,我希望它返回到完全相同的页面,使用相同的URL(/ register_from_invitation / TOKEN_HERE),但我获得new动作被展示。如果我将最后respond_with resource更改为:

respond_with resource, action: new_from_invitation

...我认为应该有效,我收到错误:Render and/or redirect were called multiple times in this action.

如何才能使用* _from_invitation路线和方法重新定位和渲染此新注册周期?这同样适用于稍后的sign_in_from_invitation路线。

感谢。

1 个答案:

答案 0 :(得分:0)

我放弃了respond_with,现在正在做:

render "new_from_invitation"

工作正常。