我正在尝试在我的 Request 类的控制器内创建一个新用户,但遇到了一些困难。下面是我在请求控制器中的“创建”操作。我意识到我不能只调用 User.new,但不确定如何构建正确的 POST 操作。
def create
@request = Request.new(params[:request])
@user = User.find_by_email(@request.email)
if @user.present?
@request.user_id = @user.id
@request.save
else
user = User.new
user.email = @request.email
user.zip = @request.zip
user.save
@request.user_id = user.id
@request.save
end
respond_to do |format|
if @request.save
UserMailer.request_confirmation(@request).deliver
UserMailer.request_notification(@request).deliver
format.html { redirect_to confirmation_url }
format.json { render json: @request, status: :created, location: @request }
else
format.html { render action: "new" }
format.json { render json: @request.errors, status: :unprocessable_entity }
end
end
end