Post Redirect Get Template in Rails

How can I implement PRG in Rails?

I used PRG in Rails, but I'm not quite sure about that. I was wondering if there is a better way to handle this in Rails?

+4
source share
6 answers

I don’t know how popular the PRG model is and why you need to religiously adhere to “redirecting” to the rejection aspect (in fact, one good reason is that sometimes you don’t want to “tune” the complexity to create a failure and keep things dry).

Basically you need to pass parameters to: user to new. I think the @Hitesh solution above is close enough.

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 
+5
source

I am not an expert in these matters, but it looks good. From what I understand, flash is part of the session. Therefore, the answers telling you to switch to a session seem a bit erroneous. In this case, you want the data to be cleared after the redirect. I'm not sure where you put it, except that it changes in the session.

As your cookie grows in size, the default session provider for Rails is a cookie in Rails 3. You could exchange the session provider if you want to save the data server. However, it is encrypted, so you are probably well versed in the data in the cookie, unless the size is a problem.

+1
source

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 
0
source

Use Session, Luke

The way you implemented it in your blog post is good enough, however you can use session instead of flash to store @user and perhaps use ActiveRecord session storage so that cookies do not bloat.

From Documentation ActionController :: Base

ActiveRecord :: SessionStore - Sessions are stored in your database, which works better than PStore with multiple application servers, and, unlike CookieStore, hides your session contents from the user. To use ActiveRecord :: SessionStore , set

 config.action_controller.session_store = :active_record_store 

in config/environment.rb and run rake db:sessions:create .

So you must ...

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

True, you should not do redirect_to '/' . You must define the root in the routes file and then make redirect_to root_path .

Edit:. Unfortunately, this was a comment on SpyrosP's answer.

Also: Here are some suggestions for excellence on flash. In particular, it can ease your mind:

A flash is a special part of a session that is cleared with every request. This means that the values ​​stored there will be available only in the next request, which is useful for storing error messages, etc.

The interesting thing is that yes, this is part of the session, so the answers to “use session instead of flash” are erroneous, as Justin Ethedge already said. Another thing is that he says that it is useful for storing messages, and not just for storing messages. With the added "etc." This would make me believe that user information is also stored as part of the intended use.

In the end, I agree with Aditya Sanghi that you should just save the user settings, and not the entire user object in flash.

0
source

I did not read the question correctly.

The validation failure that you have requires a transition to another page where another process will take place. You tried to update a domain object, it does not exist. The usual response to a validation failure is to redisplay the page, but you need to go to the create page.

Flash hash seems wrong for this. I agree with the idea of ​​entering your entered data into the session and redirecting.

0
source

All Articles