Simple rails app- how to?

I am a newbie to the ROR scene and could use some advice and guidance
from those of you will more experience.

I want to create an ice cream parlor demo app. These are the
requirements that I want to meet.

I want to track the users with cookies. The first time they visit the
site they must fill out a form that contains first name, last name,
…The user id will be auto-generated and also the cookie. This info
is stored in a Customer table. The user can also order the ice cream
from this page. This will only contain a size (drop-down box) and
checkboxes for things like sprinkles, hot fudge , etc. This info is
stored in the Preference table with CustomerId as a FK. Upon submission
of the order, I will just display that the order was placed and describe
the order.

The second time a user visits (here is where i need that cookie) I will
greet then by name and then display their last order information and
allow them to select this one, or order a new combination. This links
to the order confirmation upon submission again.

I get the concepts of ROR but i guess i just don’t know where to start
this project. I have the tables created and ready to go.

Guidance Please!!

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.

On 11/28/06, Mitch B. [email protected] wrote:

…The user id will be auto-generated and also the cookie. This info
to the order confirmation upon submission again.


Mike W.
Web D.
UW-Eau Claire

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.

On 11/28/06, Mike W. [email protected] wrote:

cookies[:user_id] = @user.id
On 11/28/06, Mitch B. [email protected] wrote:

…The user id will be auto-generated and also the cookie. This info
allow them to select this one, or order a new combination. This links


Mike W.
Web D.
UW-Eau Claire


Mike W.
Web D.
UW-Eau Claire

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

On 11/28/06, Jimmy V. [email protected] wrote:

redirect_to :action => ‘newCustomer’
object. I should use object_id instead.

Any thoughts?


Posted via http://www.ruby-forum.com/.


Mike W.
Web D.
UW-Eau Claire

Thanks- That was indeed the problem. The next snag that I am having is
passing a value from the controller into the view. For example, I want
to pull the firstName of the logged in user and then say something like
“welcome back <customer_name>”

In the controller I load in the customer info using Customer.find(:all,
:conditions => “id = cookies[:customer_id]”)

I get the right customer and i am able to extract the firstName from the
returned array. But I can’t use it in my view.

What simple concept am I not getting. I thought that an field in the
controller was available in the view. Is this a scope issue? I am
assigning the value inside the def confirm method. But I need the name
in the confirm view also.

yes i want to use a cookie in this case. But what I do not completely
understand is where and how to set the cookie. In the controller in the
index method i am doing something like this

def index
@customer = User.find(cookies[:customer_id]) if cookies[:customer_id]

if @customer.id == nil
redirect_to :action => ‘newCustomer’
else
redirect_to :action => ‘returnCustomer’
end

So I get to the newCustomer page. Now how can I set a cookie. I want
the cookie to be the newly created [customer.id] value.

I tried something like cookie[:customer_id] = @customer.id

This gives an error saying that I am assigning a value from a nil
object. I should use object_id instead.

Any thoughts?

Yeah that was it. The next struggle i am having is getting a value from
the controller to the view.

I want to use the logged in customer (as determined by the cookie)
firstName in the confirm and second order views of my app. I am able to
load the name correctly but i can not get it into the view. Is this a
scope issue.

I thought that any field in the controller was available to its view.
For example i create @customer in the “confirm” method. Then I try to
get this value in the confirm view. But no luck

What concept am I not getting here?

Hi
Customer.find() will return a Customer object containing all the
attributes of your Customer model (e.g. name, number, phone, email).
After you assign Customer to an instance variable (@customer =
Customer.find(:all, :conditions => “id = cookies[:customer_id]”)), you
may
bet the name in your view template using this embedded ruby:

Welcome <%= @customer.name %>

Hope this helps!
Dave Dumaresq.

Thanks- That was indeed the problem. The next snag that I am having is
passing a value from the controller into the view. For example, I want
to pull the firstName of the logged in user and then say something like
“welcome back <customer_name>”

In the controller I load in the customer info using Customer.find(:all,
:conditions => “id = cookies[:customer_id]”)

I get the right customer and i am able to extract the firstName from the

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]

On 11/29/06, Jimmy V. [email protected] wrote:

I get the right customer and i am able to extract the firstName from the


Mike W.
Web D.
UW-Eau Claire

Jimmy V. 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