<?xml version="1.0" encoding="UTF-8"?>
<page>
  <content>As I've done more development with Rails I've become more interested in refactoring and simplifying my code.  One of the ways I've found to do that is to combine actions in object creation.  This means going from having separate "new" and "create" actions to just having single "new" action.

How? The wonder of @request.post?@.

So let's say I have an action "new" that lets a user fill out a form to create a new Widget.  It might look something like this:

&lt;pre&gt;
&lt;%= start_form_tag :action =&gt; "create" %&gt;
  &lt;%= text_field "widget", "name"  %&gt;
  &lt;%= submit_tag "create" %&gt;
&lt;%= end_form_tag %&gt;
&lt;/pre&gt;

With the corresponding actions in the controller:

&lt;pre&gt;
def new
  @widget = Widget.new
end

def create
  @widget = Widget.new(params[:widget])
  @widget.save
end
&lt;/pre&gt;

So wouldn't it be easier to just have one action for this whole process?  I'd like to think so.  Here's the combined solution:

Change our form action:

&lt;pre&gt;
&lt;%= start_form_tag :action =&gt; "new" %&gt;
&lt;/pre&gt;

And combine our controller actions:

&lt;pre&gt;
def new
  @widget = Widget.new

  if request.post?
    @widget = Widget.new(params[:widget])
    @widget.save
  end
end
&lt;/pre&gt;

Now our form redirects to itself, but it passes along the user entered form information as a post request.  By checking for the presence of the post information in the request we can determine if the call of the "new" action is the first initial load or a user submit click.

The result is one less action in the controller and a simpler process for creating a new Widget.  The process is the same for combining Edit/Update actions into a single Edit action.</content>
  <created-on type="datetime">2006-05-20T23:14:47+00:00</created-on>
  <has-changes type="integer">0</has-changes>
  <html>&lt;p&gt;As I&amp;#8217;ve done more development with Rails I&amp;#8217;ve become more interested in refactoring and simplifying my code.  One of the ways I&amp;#8217;ve found to do that is to combine actions in object creation.  This means going from having separate &amp;#8220;new&amp;#8221; and &amp;#8220;create&amp;#8221; actions to just having single &amp;#8220;new&amp;#8221; action.&lt;/p&gt;


	&lt;p&gt;How? The wonder of &lt;code&gt;request.post?&lt;/code&gt;.&lt;/p&gt;


	&lt;p&gt;So let&amp;#8217;s say I have an action &amp;#8220;new&amp;#8221; that lets a user fill out a form to create a new Widget.  It might look something like this:&lt;/p&gt;


&lt;pre&gt;
&amp;lt;%= start_form_tag :action =&amp;gt; "create" %&amp;gt;
  &amp;lt;%= text_field "widget", "name"  %&amp;gt;
  &amp;lt;%= submit_tag "create" %&amp;gt;
&amp;lt;%= end_form_tag %&amp;gt;
&lt;/pre&gt;

	&lt;p&gt;With the corresponding actions in the controller:&lt;/p&gt;


&lt;pre&gt;
def new
  @widget = Widget.new
end

def create
  @widget = Widget.new(params[:widget])
  @widget.save
end
&lt;/pre&gt;

	&lt;p&gt;So wouldn&amp;#8217;t it be easier to just have one action for this whole process?  I&amp;#8217;d like to think so.  Here&amp;#8217;s the combined solution:&lt;/p&gt;


	&lt;p&gt;Change our form action:&lt;/p&gt;


&lt;pre&gt;
&amp;lt;%= start_form_tag :action =&amp;gt; "new" %&amp;gt;
&lt;/pre&gt;

	&lt;p&gt;And combine our controller actions:&lt;/p&gt;


&lt;pre&gt;
def new
  @widget = Widget.new

  if request.post?
    @widget = Widget.new(params[:widget])
    @widget.save
  end
end
&lt;/pre&gt;

	&lt;p&gt;Now our form redirects to itself, but it passes along the user entered form information as a post request.  By checking for the presence of the post information in the request we can determine if the call of the &amp;#8220;new&amp;#8221; action is the first initial load or a user submit click.&lt;/p&gt;


	&lt;p&gt;The result is one less action in the controller and a simpler process for creating a new Widget.  The process is the same for combining Edit/Update actions into a single Edit action.&lt;/p&gt;</html>
  <id type="integer">26</id>
  <is-post type="integer">1</is-post>
  <is-published type="integer">1</is-published>
  <title>How To Combine Form Actions in Rails</title>
  <url>notebook/2006/05/20/how_to_combine_form_actions_in_rails</url>
</page>
