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
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…?
oops missed an extra ‘end’ out there to close the main if statement
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.
On Feb 14, 2:11 pm, Phil T. [email protected]