laredotornado-8iDLEspWrrZBDgjK7y7TUQ@public.gmane.org
2008-Feb-14 19:43 UTC
How can I dynamically create this form?
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| %>
<div id="ec_line_items">
<%= render :partial => ''ec_line_item'',
:collection =>
@ec_order.ec_line_items %>
</div>
<%= 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
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---
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....?
--
Posted via http://www.ruby-forum.com/.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---
oops missed an extra ''end'' out there to close the main if statement -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
laredotornado-8iDLEspWrrZBDgjK7y7TUQ@public.gmane.org
2008-Feb-14 23:30 UTC
Re: How can I dynamically create this form?
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 Tayo
<rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org>
wrote:> oops missed an extra ''end'' out there to close the main if
statement
> --
> Posted viahttp://www.ruby-forum.com/.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk-unsubscribe@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---