simple rails app- how to?

storing and reading information from cookies is incredibly simple in Rails. much like storing information in the session, the syntax for using cookies is

cookies[:key_name] = “Vicky Visitor”

So if all you care about is the user, you can do something like this:

cookies[:user_id] = @user.id

and when you’re ready to retrieve the information:

@user = User.find(cookies[:user_id]) if cookies[:user_id]

once you have the user, you should be able to figure out what their last order was, their name, etc.

I just realized something: that’s assuming you want to use cookies (want to remember the person the next time they visit your site). If you want to only remember them while they’re visiting, you should be doing the same thing, except using the session instead.

oh, I think I see your problem. Either you never initialized @customer or you never saved it to the database. You’ll need to do this:

@customer = Customer.new
@customer.save

  • or -

@customer = Customer.create

Either way you have to save your customer to the database first. Otherwise you won’t have an ID to save to the cookie

Jimmy Voegeli wrote:

I thought that any field in the controller was
available to its view.

Any instance variable (i.e., a variable name prefaced by '@') created in the controller will be available in it's view.

For example i create @customer in the
"confirm" method. Then I try to get this
value in the confirm view. But no luck

Post the method and view code. It's something simple.

Best regards,
Bill

I think the problem that you’re running into is that find(:all) returns an array of customers (in this case only one), not a single instance of a customer like you need. And if you’re doing a search by an id, you don’t need to use conditions

[controller]
@customer = Customer.find(cookies[:customer_id])
[/controller]

[view]
Welcome <%= @customer.name %>
[/view]