1

嘿,我正在制作一个具有邀请系统的应用程序。当您被邀请时,您会收到一封电子邮件,允许您使用该特定电子邮件注册为用户。因此,我希望我的表单 (invitation#show) 允许受邀人员完成注册,但电子邮件字段除外。就像我已经知道你的电子邮件一样。但它总是告诉我电子邮件不能为空。

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

邀请控制器

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 

邀请#show

= 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
4

1 回答 1

1

与其以单独的形式处理受邀用户的创建,不如仍然使用 users#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 -%>

最后,在 users#create 中处理邀请令牌:

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 是否已经在那里使用

于 2013-06-26T01:00:03.853 回答