I haven't seen many examples of it being used, I think it's because of
the validation of the models. We need to keep the data the user
submitted in the form and show validation errors, so the we'd need to
store it somewhere to survive the redirect (flash could potentially be
used).
Here's an example from the scaffolding.
# POST /posts
# POST /posts.xml
def create
@post = Post.new(params[:post])
respond_to do |format|
if @post.save
flash[:notice] = 'Post was successfully created.'
format.html { redirect_to(@post) }
else
format.html { render :action => "new" }
end
end
end
So in this example I'd want the else to redirect_to the "new" action
instead of render, but where am I going to store the @post's state.
Anyways I was hoping someone found a good solution to this in Rails but
I haven't seen anyone talk about the pattern.
--
Posted via http://www.ruby-forum.com/.
> The wikipedia article uses the "fact" that a user may try to
> bookmark that page (after the create) and therefore not be getting
> the /posts/new but rather /posts. imo, it's a bunch of bollocks and
> redirect on success coupled with render on failure is much more
> simpler and therefore better for you. What kind of user would
> bookmark a posts/new page anyway?
>
Those damn clueless users! One of our internal apps is the one that
handles user authentication for the people that work for us; and one
person is in charge of creating these accounts. wouldn't be surprised
at all if they bookmarked the new user page. I don't have a particular
opinion about this redirect thingy, but it's a slippery slope when you
start saying things like 'what kind of user would do x'. It's easy to
end up with an app that does exactly what you want, but not
necessarily what the end user wants
Fred
> "If you make a fool proof application, only fools will use it."
>
Well there are a lot of fools out there, and it's probably easier to
get money from them than from smart people so that might be a good
business model.
Joking aside, my point was that there's a middle-ground and
bookmarking a page that you use often hardly seems outlandish (it is
after all the point of them).
Here's my first try:
def new
@post = Post.new(flash[:post])
if flash[:post]
@post.valid?
flash[:post] = flash[:post] #Save it in the flash again to survive
a refresh
end
respond_to do |format|
format.html # new.html.erb
end
end
def create
@post = Post.new(params[:post])
respond_to do |format|
if @post.save
flash[:notice] = 'Post was successfully created.'
format.html { redirect_to(@post) }
else
flash[:post] = params[:post]
format.html { redirect_to(new_post_path) }
end
end
end
The only downside of doing it this way I can think of is that the cookie
stored session could overflow with a big enough post params. Then I
guess we'd have to switch to a different session store type (Memcache,
DB, or file based)
>
> So if I don't want to loose the fool population with my app, what
> would
> be the best way of adopting the post-redirect-get pattern on all post
> operations?
I'd store post.attributes rather than post. You don't want to be
storing complex objects in the session if you can avoid it.
Fred