Re: session question..

Frederick Cheung
Fri, 10 Oct 2008 12:46:50 -0000

On 10 Oct 2008, at 12:13, Jay Pangmi wrote:

>
> Frederick Cheung wrote:
>> On Oct 10, 11:46�am, Jay Pangmi
<rails-mailing-list-ARtvInVfO7m5VldFQK4jKA@public.gmane.orgt>
>
>> The same as retrieving any other value from the sesion (ie
>> session[:cart])
>>
>> Fred
>
> Actually, params[:cart] is derived from <%form_for :cart ...%> Inside
> this, there are textfields corresponding to "carts" table and
hence I
> could save the values in the form and I''m doing this way:
>
> CONTROLLER:
> def add_to_cart
>    @cart_items=[]
>    @cart=find_cart(params[:cart])
>    if @cart.save
>      @cart_items << @cart
>    else
>      flash[:notice]="Details couldn''t be saved"
>    end
> end
>
> private
>
>  def find_cart(cartobj)
>    session[:cart] ||= Cart.new(cartobj)
>  end
>
> I''m using @cart_items array just because I couldn''t
iterate through
> the
> @cart to list all the entered details in the add_to_cart.rhtml as:
>

This doesn''t seem to make any sense. For example the @cart_items array
will only ever contain 1 item, you are storing the whole cart object
in the session (what''s the point of storing it in the database and the
session ?)

> VIEW:
> Your details:
>
>  <%for item in @cart_items%>
> <%=item.campsite%>
> <%=item.arrival_date%>
> <%=item.nights%>
> <%=item.parents%>
> <%=item.children%>
> <%=item.family_claim%>
>  <%end%>
>
> <%=button_to "Empty cart", :action => :empty_cart%>
>
> So, now when I click the "add to cart" button in the view
everything
> gets added to the "carts" table and I''m taken to
add_to_cart.rhtml
> with
> the list of details. Now in this case how can i empty it when
> empty_cart
> action is triggered for this specific session?
>
Actually if you check your code you''ll find that beyond the first time
you call add_to_cart it won''t actually do anything.

Is you cart object actually an item in a cart (rather than the
container, as the name usually implies) ?

Fred

> thanks fred.
> --
> 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@googlegroups.com