应该使用哪个?flash[:notice] = 'some msg'
还是 :notice = 'some msg'
?
我遇到了这两个选项,但没有解释它们之间的区别。所以我不确定该使用哪一个。也许它们是相同的吗?
应该使用哪个?flash[:notice] = 'some msg'
还是 :notice = 'some msg'
?
我遇到了这两个选项,但没有解释它们之间的区别。所以我不确定该使用哪一个。也许它们是相同的吗?
def create
@activity = Activity.new activity_params
if @activity.save
flash[:notice] = 'Activity was successfully created!'
redirect_to activity_path(@activity)
else
render :new
end
end
然后你可以使用flash[:notice]
来设置flash
哈希的通知为Activity was successfully created!
。
然而,当使用redirect_to
时,您可以将:notice
作为选项传递,如下:
def destroy
if @activity.destroy
redirect_to activities_path, notice: 'Activity was successfully destroy!'
else
flash.now[:notice] = 'Activity was not destroyed.'
end
end
如果您查看ActionController::Redirecting模块,您会发现可以将通知作为选项传递。
从根本上说,这两个东西做的是同样的事情。只是设置方式不同。
希望这能帮到您!
redirect_to
仅支持alert
、notice
和flash
。它不支持success
和其他类型。notice
也存储在名为notice
的变量中。因此,您无需使用flash
对象来访问它。 - konyak