Post Redirect Get pattern in Rails

前端 未结 6 1366
隐瞒了意图╮
隐瞒了意图╮ 2021-01-05 15:19

How can I implement PRG in Rails?

I used PRG in Rails, but I am not totally convinced it\'s right. I was wondering is there any better way to handle it in Rails?

6条回答
  •  清歌不尽
    2021-01-05 15:53

    use below code

    class UsersController < ApplicationController
    
      def new
        @user = User.new(session[:user_param])
       session[:user_param]=nil
      end
    
      def create
    
        @user = User.new(params[:user])
    
        if @user.save
          # clears previously stored user if there is any
          flash.discard(:user)
          redirect_to '/'
        else
          session[:user_param] = @user
          redirect_to :action => :new
        end
    
      end
    
    end
    

提交回复
热议问题