嘿,我正在制作一个具有邀请系统的应用程序。当您被邀请时,您会收到一封电子邮件,允许您使用该特定电子邮件注册为用户。因此,我希望我的表单 (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