remembering address details and visited page

I actually have 2 questions: 1. Right now when a customer logs in, he gets redirected to the 1st page in the catalog, and the cart session is stored in the database. How can I redirect the customer to his last visited page at login?

2. When the same customer checks out, he needs to fill out his address. There is a model for customers, which the administrator controls. The administrator can enter the customer's email, address, phone and so on. How can I make that the details that are on file, get remembered and automatically filled out in the checkout form (so the customer does not have to enter them again and again)?

The models are: cart - controls checkout order customer ...

Relationships in the Cart class are:   has_many :cart_items   has_many :products, :through => :cart_items   belongs_to :customer

An example from my checkout form: <p><label for="order_email">Email</label></p> <p><%= text_field :order, :email %></p>

Any help will be much appreciated. Cheers, Elle

I actually have 2 questions: 1. Right now when a customer logs in, he gets redirected to the 1st page in the catalog, and the cart session is stored in the database. How can I redirect the customer to his last visited page at login?

You have to store it somewhere, in the database or in a cookie perhaps?

2. When the same customer checks out, he needs to fill out his address. There is a model for customers, which the administrator controls. The administrator can enter the customer's email, address, phone and so on. How can I make that the details that are on file, get remembered and automatically filled out in the checkout form (so the customer does not have to enter them again and again)?

The models are: cart - controls checkout order customer ...

Relationships in the Cart class are:   has_many :cart_items   has_many :products, :through => :cart_items   belongs_to :customer

An example from my checkout form: <p><label for="order_email">Email</label></p> <p><%= text_field :order, :email %></p>

Are you actually storing an email address with every order? Seems like you'd just want to store the user's id of who placed the order. Then you can get the email from that user record.

Are you actually storing an email address with every order? Seems like you'd just want to store the user's id of who placed the order. Then you can get the email from that user record.

Yes, I do. And shipping address and... Could I use something like that: <p><label for="order_email">Email</label></p> <p><%= text_field_tag "email", "#{customer.email}" %></p>

instead of: <p><label for="order_email">Email</label></p> <p><%= text_field :order, :email %></p>

Would Rails know to find that info? and would it still know where that info should be stored?

Elle

Actually this gave me an error, but when changing it to: <%= text_field_tag "email", @cart.customer.email %> it worked. Still one last question: will rails know that I want to save that email address in the orders table? And also, trying to checkout, I now get an error:

There were problems with the following fields: Email is invalid

... but the email is valid. What is the problem then?

Elle

You’re using a text_field_tag, try specifying <%= text_field “order”, “email”, @cart.customer.email %> instead.

This gets an error:

TypeError in Cart#checkout Showing app/views/cart/checkout.rhtml where line #17 raised: can't convert Symbol into String

17: <p><%= text_field "order", "email", @cart.customer.email %></

Elle

If I use this version: <%= text_field_tag "email", @cart.customer.email %> The email gets displayed and when I use debug to check type, the type of @cart.customer.email is String. But then I get an error that the email address is invalid, even though it is.

If I use: <%= text_field "order", "email", @cart.customer.email %> I get: TypeError in Cart#checkout Showing app/views/cart/checkout.rhtml where line #17 raised: can't convert Symbol into String

Would anyone have a solution? or know what the problem is?

Thanks, Elle

Is there an @order instance variable defined?

What is the backtrace on that error?

No @order instance. The order is created and populated once the user clicks "process".

Application trace is: #{RAILS_ROOT}/vendor/rails/actionpack/lib/action_view/helpers/ form_helper.rb:162:in `delete' #{RAILS_ROOT}/vendor/rails/actionpack/lib/action_view/helpers/ form_helper.rb:162:in `text_field' #{RAILS_ROOT}/app/views/cart/checkout.rhtml:18:in `_run_rhtml_47app47views47cart47checkout46rhtml'

Is this any help? Elle

Indeed it is, define @order = Order.new in the controller and it might get rid of your problem. What it’s trying to do is to find an order instance variable with an email method on it, but maybe because you don’t have it defined it’s throwing that error.

This is my cart_controller#place_order:

  def place_order     @page_title = "Checkout"     @order = Order.new(params[:order])     @order.customer_ip = request.remote_ip     @order.customer_id = @customer.id     @order.discount = @customer.discount     populate_order

     if @order.save       if @order.process         flash[:notice] = 'Your order has been submitted, and will be processed immediately.'         session[:order_id] = @order.id         # Empty the cart         @cart.cart_items.destroy_all         redirect_to :action => 'thank_you'       else         flash[:notice] = "Error while placing order. '#{@order.error_message}'"         render :action => 'view_cart'       end     else       render :action => 'checkout'     end   end

Should I define it in another place?

Elle

Between the two of us, I would be the stupid one, not you :slight_smile: And I learn something new every day. So, I can use :value to specify what shows in the field.

But I now have a new question: I have all the states in an array and I use an iterator to print out all the states. Now, I want the customer's state to be pre-selected, so, my logic says to use something like that: states.each do | state | %>      <option value="<%= state %>"      <%= if state == @cart.customer.ship_to_state then puts 'selected="selected"' end %> ><%= state %></option>     <% end %> ...but this doesn't work. It just prints the normal list of states, with none selected. What should I change?

Elle

Why wouldn't you do something along the lines of:

# your_controller.rb @some_states_and_provinces = ['alabama', 'arkansas', 'canberra', 'new
south wales'].map{|s| [s,s]} @selection = 'canberra'

# your_view.rhtml <%= select('order', 'state',
@some_states_and_provinces, :first, :first, @selection) %>

I don't know what your fields are, but chances are there's a Rails
helper to take care of your problem.

Between the two of us, I would be the stupid one, not you :slight_smile: And I learn something new every day. So, I can use :value to specify what shows in the field.

But I now have a new question: I have all the states in an array and I use an iterator to print out all the states. Now, I want the customer's state to be pre-selected, so, my logic says to use something like that: states.each do | state | %>     <option value="<%= state %>"     <%= if state == @cart.customer.ship_to_state then puts 'selected="selected"' end %> ><%= state %></option>    <% end %> ...but this doesn't work. It just prints the normal list of states, with none selected. What should I change?

Don't use puts - <%= if state == @cart.customer.ship_to_state then
'selected="selected"' end %> or even <%= 'selected="selected"' if state == @cart.customer.ship_to_state%>

But as it's been said before there are helpers to take care of this.

Fred

Thanks guys and I agree about using a helper method. Would be better... but I tried that before and because I don't know enough about rails just yet, I had trouble with it and resorted to this solution. How would you suggest I create a helper method?

Elle