Rails tutorial of SendGrid的样板代码如下所示:
class UsersController < ApplicationController
def create
# Create the user from params
@user = User.new(params[:user])
if @user.save
# Deliver the signup email
UserNotifier.send_signup_email(@user).deliver
redirect_to(@user, :notice => 'User created')
else
render :action => 'new'
end
end
end
但是对于我来说redirect_to(@user, :notice => 'User created')
之类的问题导致了问题 - 我猜是因为我自己的数据库没有以这种方式设置。
然后我做的是用render :new unless @user.save
替换它,因为那是我的旧创建方法的样子,即它看起来像这样:
# def create
# @user = User.new(user_params)
# render :new unless @user.save
# end
现在我的功能如下:
def create
# Create the user from params
@user = User.new(user_params)
if @user.save
# Deliver the signup email
UserNotifierMailer.send_signup_email(@user).deliver
# redirect_to(@user, :notice => 'User created')
render :new unless @user.save
else
# render :action => 'new'
render :new unless @user.save
# is putting "render :new unless @user.save" detrimental? does it
# actually solve the problem of saving that user?
end
end
这合理吗?我的意思是 - 不可否认它是一种hacky - 虽然它很糟糕吗?
答案 0 :(得分:1)
在redirect
对象后使用create
:
def create
# Create the user from params
@user = User.new(user_params)
if @user.save
# Deliver the signup email
UserNotifierMailer.send_signup_email(@user).deliver
redirect_to new_user_path # or another path what you wish
else
render :new
end
end
如果您使用render
,则仅渲染视图,如果redirect_to
- 您将通过操作进行查看