在 Rails 中发布重定向获取模式

编程入门 行业动态 更新时间:2024-10-10 19:26:20
本文介绍了在 Rails 中发布重定向获取模式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

如何在 Rails 中实现 PRG?

How can I implement PRG in Rails?

我在 Rails 中使用了 PRG,但我并不完全相信它是正确的.我想知道在 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?

推荐答案

我不知道 PRG 模式有多流行,以及为什么人们必须虔诚地坚持重定向"来解决它的失败方面(实际上,一个很好的理由有时您不想处理创建失败时的设置"复杂性并保持干燥).

I don't know how popular PRG pattern is and why one has to religiously stick to the "redirect" on failure aspect of it (actually, one good reason is sometimes you dont want to deal with the "setup" complexity at create failure and keep things dry).

您基本上需要的是将 :user 的参数转移到新的.我认为上面@Hitesh 的解决方案非常接近.

What you basically need is to transfer the params for :user to new. I think @Hitesh's solution above is quite close.

class UsersController < ApplicationController def new if flash[:user_params] @user = User.new(flash[:user_params]) @user.valid? else @user = User.new end end def create @user = User.new(params[:user]) if @user.save # clears previously stored user if there is any flash[:notice] = "User created." redirect_to '/' else flash[:error] = "Error saving User" flash[:user_params] = params[:user] redirect_to :action => :new end end end

更多推荐

在 Rails 中发布重定向获取模式

本文发布于:2023-10-12 05:09:53,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1483802.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:重定向   模式   Rails

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!