Reputation: 3233
Suggest me which of the following flow is efficient ?
flow 1: Happy Flow: form_page -----(form submit)------> post_action ----------(success)------------> redirect_to_success_page
Error Flow: form_page -----(form submit)------> post_action ----------(failure)------------> redirect with params to form_page Note: In this flow end user will see the params in the url except password (But it looks ugly for me ).
So I changed the flow from redirect page to render page. I created dynamic class in model that will act like active record object (It is not a sub class to active record). After modification I will be able to see the field values in the form but not in the url params. So the flow will be like the following
Happy Flow: form_page -----(form submit)------> post_action ----------(success)------------> redirect_to_success_page
Error Flow: form_page -----(form submit)------> post_action ----------(failure)------------> render form_page (view))
Note: In this flow the end user will not see the params in the url. But he will see a different url instead of form_url url i.e we will see 'post_action' in the url because we are just rendering the form_page view in post_action action. Right now when you refresh the page i reset the values in object (but this can be changed if we want the obj to be alive all the time)
Apart from this details if you want code I will paste here.
controller.rb
-------------
def form_page
@something = SomeName.new({:one=>'', :two => ''})
end
def post_action
@something = SomeName.new(params[:some_variable])
if success
redirect_to success_url
else
render :action => form_page
end
end
model.rb
--------
class SomeName
def initialize(hash)
hash.each do |k,v|
self.instance_variable_set("@#{k}", v)
self.class.send(:define_method, k, proc{self.instance_variable_get("@#{k}")})
self.class.send(:define_method, "#{k}=", proc{|v| self.instance_variable_set("@#{k}", v)})
end
end
end
view:
-----
<% form_for :something, :url => {:action => 'post_action'} do |f| %>
<%= f.text_field :one %>
<%= f.text_field :two %>
<% end %>
Thanks in Advance, Arun.
Upvotes: 0
Views: 103
Reputation: 24340
A classic practice in Rails is to have same url do display the form and validate the form (with GET method in the first case, POST or PUT in the second case). Use it with the second flow of your question: after an error, the url of the user has not changed after the error, and the parameters are not in the URL.
Upvotes: 1