# How can I dynamically create this form?

**URL:** <https://www.ruby-forum.com/t/how-can-i-dynamically-create-this-form/129143>\
**Category:** Rails\
**Created:** [February 14, 2008, 8:43pm UTC](https://www.ruby-forum.com/t/how-can-i-dynamically-create-this-form/129143 "2008-02-14T20:43:49Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [February 14, 2008, 8:43pm UTC](https://www.ruby-forum.com/t/how-can-i-dynamically-create-this-form/129143/1 "2008-02-14T20:43:49Z")

</div>

Hi,

I have a model, ec\_order, with many of another model, ec\_line\_item. I  
have a page where users can dynamically add line items to their  
order.

===============Begin new.rhtml==================  
\<% form\_for :ec\_order, :url =\> ‘summary’ do |f| %\>

  
\<%= render :partial =\> ‘ec\_line\_item’, :collection =\>  
@ec\_order.ec\_line\_items %\>  

  
\<%= add\_item\_link “Add an item” %\>  
\<%= submit\_tag(“Submit Form”) %\>  
\<% end %\>  
===============End new.rhtml====================

If a user submits an order and there are some problems, I redirect  
them back to the page above. My question is, how can I make sure the  
user sees the same number of items that they had when they submitted  
the form?

Here are the two controller methods in question.

```
    def new
            @user = User.find(session[:user_id])
            1.times { @ec_order.ec_line_items.build }
    end

    def summary
            @ec_order = EcOrder.new(params[:ec_order])
            if (!@ec_order.valid?)
                    flash[:notice] = "Order is invalid."
                    redirect_to :action => 'new'
            else
                    session[:ec_order] = @ec_order
            end
    end

```

Thanks, - Dave

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [February 14, 2008, 9:11pm UTC](https://www.ruby-forum.com/t/how-can-i-dynamically-create-this-form/129143/2 "2008-02-14T21:11:14Z")

</div>

hi Dave,

you could try this

instead of submitting to url=\>“summary”, take that line out…so that it  
will submit to new.rhtml automatically…then make your new action look  
something like this:

```
   def new
            @user = User.find(session[:user_id])
            if request.post?
                 @ec_order = params[:ec_order]
                 if(!@ec_order.valid?)
                    flash[:notice] = "Order is invalid"
                 else
                    session[:ec_order] = @ec_order
                 end

           1.times { @ec_order.ec_line_items.build } <<<< sorry not 

```

sure what this line does  
end

Re the session[:ec\_order] thing, I’m not sure why you’re doing that, do  
you know that if the variable in the action is an instance variable,  
i.e. has an @ at the front, then it can be seen in the view…?

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [February 14, 2008, 9:11pm UTC](https://www.ruby-forum.com/t/how-can-i-dynamically-create-this-form/129143/3 "2008-02-14T21:11:35Z")

</div>

oops missed an extra ‘end’ out there to close the main if statement

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [February 15, 2008, 12:30am UTC](https://www.ruby-forum.com/t/how-can-i-dynamically-create-this-form/129143/4 "2008-02-15T00:30:45Z")

</div>

Hi Phil,

Thanks for your input. The reason I had an intermediate “summary”  
screen was because I wanted the user to be able to confirm what they  
were ordering before they locked themselves in. I would prefer to  
keep the summary view code and the new view code separate, but if the  
only way to get what I want is to combine them, I’m willing to do  
that.

The reason for this line

1.times { @ec\_order.ec\_line\_items.build }

which I got from a Railscast tutorial, is so that when the user first  
sees the order form page, there is at least one line item displayed.  
If I remove this line, there are no items displayed.

- Dave

On Feb 14, 2:11 pm, Phil T. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)
