使用其他控制器创建用户

时间:2013-06-26 00:22:42

标签: ruby-on-rails ruby ruby-on-rails-3

嘿,我正在制作一个拥有邀请系统的应用。当您受到邀请时,您会收到一封电子邮件,您可以使用该特定电子邮件 以 用户身份进行注册。因此,我希望我的表单(邀请#show)允许受邀人员完成注册,但电子邮件字段除外。我是如何知道你的电子邮件的。但它总是告诉我电子邮件不能空白。

所以我在想,当我尝试创建一个用户(邀请#show)时,它总是回到用户#create controller。这是我的问题,如果有的话,我有什么方法可以改变它吗?

邀请控制器

class InvitationsController < ApplicationController
    def new
        @invitation = Invitation.new
    end

  def create
  @invitation = current_user.invitations.new(params[:invitation])
    if @invitation.valid?
          temp_email = @invitation.email
        if User.find_by_email(temp_email)
          flash.now.alert = "This email was already invited!"
          render "new"
        else
          @invitation.save
          redirect_to root_url, :notice => "Invitation sent!"
          UserMailer.invitation(@invitation).deliver
      end
    else
      render "new", :notice => "Somehting went wrong!"
    end
end

 def show
    @invitation = Invitation.find_by_invite_token(params[:invite_token])
    @user.email = @invitation.email
    if @user.save 
      @user.email_activation_token = true
      cookies[:auth_token] = user.auth_token
      redirect_to root_url, :notice => "Welcome!"
    else
      render "new", :notice => "Something went wrong!"
    end
 end

  def accept_referral
      @invitation = Invitation.find_by_invite_token(params[:invite_token])
      @invitation.accepted_at = Time.zone.now
      @invitation.save!
      if cookies[:auth_token]
        cookies.delete(:auth_token)
      end
      @user = User.new
      render "show"
  end
end

用户控制器

class UsersController < ApplicationController
  before_filter :admin_and_user, only: [:destory, :edit, :show]
  before_filter :admin_user, only: :index
  def new
    @user = User.new
  end
  def create
    @user = User.new(params[:user])
    if @user.save
      UserMailer.registration_confirmation(@user).deliver
      redirect_to root_url, :notice => "Signed up!"
    else
      render "new"
    end
  end 

邀请#展示

= form_for @user do |f|
  - if @user.errors.any?
    .error_messages
      %h2 Form is invalid
      %ul
        - for message in @user.errors.full_messages
          %li
            = message
  %p
    = f.label :name
    = f.text_field :name
  %p
    = f.label :password
    = f.password_field :password
  %p
    = f.label :password_confirmation
    = f.password_field :password_confirmation

  %p.button
    = f.submit

1 个答案:

答案 0 :(得分:1)

而不是以分离的形式处理受邀用户创建,也许最好还是在其中使用#new和处理params [:invite_token]的用户,例如:

def new
  @user = User.new
  @invitation = Invitation.find_by_invite_token(params[:invite_token]) if params[:invite_token].present?
end

并且表单应该有一个hidden_​​field和条件来删除email_field,例如:(在erb中因为我对haml不熟悉)

<% if @invitation.nil? %>
  <%= f.email_field :email %>
<% else %>
  <%= hidden_field_tag :invite_token, @invitation.invite_token %>
<% end -%>

最后,在用户#create:

中处理invite_token
def create
  @invitation = Invitation.find_by_invite_token(params[:invite_token]) if params[:invite_token].present?
  params[:user][:email] = @invitation.email unless @invitation.nil?
  @user = User.new(params[:user])
  if @user.save
    UserMailer.registration_confirmation(@user).deliver
    redirect_to root_url, :notice => "Signed up!"
  else
    render "new"
  end
end

您可能还想将@invitation代码取出到before_filter以使事情变得整洁,并检查@ invitation.email是否已在其中使用