Ruby on Rails: How would i stay on the same page if the post is not saved?

前端 未结 3 786
小鲜肉
小鲜肉 2021-02-19 03:59
def create
    @addpost = Post.new params[:data]
    if @addpost.save
        flash[:notice] = \"Post has been saved successfully.\"
        redirect_to posts_path
    e         


        
3条回答
  •  野性不改
    2021-02-19 04:17

    flash.now with render is what you're looking for.

    flash.now[:notice] = "Post can not be saved, please enter information."
    render :new
    

    Also instead of

    flash[:notice] = "Post has been saved successfully."
    redirect_to posts_path
    

    you can just write

    redirect_to posts_path, :notice => "Post has been saved successfully."
    

    and it will do the same thing. It works only with redirect_to though, not with render!

提交回复
热议问题