Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/62.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/26.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 注意:保存用户模型时触发两次_Ruby On Rails - Fatal编程技术网

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 %>