Ruby on rails 注意:保存用户模型时触发两次
当我保存一个新用户时,通知会触发两次。关于如何让它一次开火有什么建议吗?我也在使用魔法Ruby on rails 注意:保存用户模型时触发两次,ruby-on-rails,Ruby On Rails,当我保存一个新用户时,通知会触发两次。关于如何让它一次开火有什么建议吗?我也在使用魔法 def create @user = User.new(user_params) if @user.save redirect_to @user, notice: 'Profile successfully created.' auto_login(@user) else render :new end
def create
@user = User.new(user_params)
if @user.save
redirect_to @user, notice: 'Profile successfully created.'
auto_login(@user)
else
render :new
end
end
闪光: flash提供了一种在动作之间传递临时对象的方法。你在闪光灯中放置的任何东西都将暴露在下一个动作中,然后被清除 根据您的代码,通知不能触发两次。在你的应用程序中,这个代码有两次(我猜是在你的布局文件中,然后是你的视图中)
“flash{name}”%>
根据上面给出的代码片段,我认为通知不会出现两次。您的代码中没有任何错误。有一种可能的情况是,在应用程序中
布局显示通知定义了一次,而在当前视图模板中又定义了一次。这就是为什么它会显示两次。我认为你的应用程序中有多个闪存块,会显示两次通知
<% flash.each do |name, msg| %>
<%= content_tag :div, msg, :id => "flash_#{name}" %>
<% end %>