I am doing the "depot" application from Agile Web Development With
Rails book - and there is a lot of copy-and-paste. And I have to say,
as a beginner in both Ruby and Rails, I don't understand quite a lot
of things.
Let's talk about this thing - there is a method called "add_to_cart".
I don't understand much, what it's doing... (it's in class
StoreController < ApplicationController)
def add_to_cart
product = Product.find(params[:id])
@cart = find_cart
@cart.add_product(product)
redirect_to(:action => 'display_cart')
end
So, I have a few questions.
1) what exactly is product? Why it's not @product? Is it global or
local variable?
2) what exactly is Product? I'm not sure, but I think its the only
instance of Product class, right? Then why is the name of instance and
name of the class the same?
3) What exactly is this params thing?
4) what exactly is :id? I don't understand, in which cases you use
colon and in which not - why I have to use it in Product.find, but not
in add_product?
5) what exactly does => mean? Why I can't use ordinary = ?
Sorry if I ask too much, I just want to understand this language and
this framework.
Kaja Bilek
> 3) What exactly is this params thing?
>
params is the parameter hash passed when the request is made, e.g. when
you submit a form your data comes in through params - ditto when you
make a GET request via an URL (/user/23 for example)
> 4) what exactly is :id? I don't understand, in which cases you use
> colon and in which not - why I have to use it in Product.find, but not
> in add_product?
>
the preceding colon signifies a symbol - there's some good info on ruby
symbols here =>
http://glu.ttono.us/articles/2005/08/19/understanding-ruby-symbols &&
here => http://www.troubleshooters.com/codecorn/ruby/symbols.htm
> 5) what exactly does => mean? Why I can't use ordinary = ?
>
key=>value, pretty common across different languages
> Sorry if I ask too much, I just want to understand this language and
> this framework.
>
>
perhaps have a look @ http://pine.fm/LearnToProgram/ - a tasty little
morsel that should help - not the most comprehensive answer but it's 2am
and i've been dipping the whiskey :P
> Kaja Bilek
>
>
> >
>
>
So when does product refer to my product model? And what exactly is
the difference between the local variable w/ @ and without. I thought
there weren't a whole lot of variables without the @.
> perhaps have a look @http://pine.fm/LearnToProgram/- a tasty little
A local variable without the @ is simply that - a variable local to the
specific method in your controller. With the '@', it is an instance
variable. Without going into the meanings of instance variables, the
"Rails" meaning (not the Ruby one) for the variable is that it can be
seen by code in both the controller and the view.
So, a temporary variable in a method in your controller would be without
a '@' - any variable which you would like to display in the view must be
an instance variable.
If we look at the code in the original post,
def add_to_cart
product = Product.find(params[:id])
@cart = find_cart
@cart.add_product(product)
redirect_to(:action => 'display_cart')
end
we can see that the details of the product that you find do not need to
be displayed in the view. But, the cart details must be shown in that
view. That's why product (without @) and @cart (with @).
Lastly, params (though you didn't ask) is a hash that communicates
parameters from to an action. Think of it as input parameters to the
controller's method.
In summary, the controller method picks up the inputs from the params
hash, processes it in local variables (without @) and writes its output
to instance variables (with @). The view that is rendered as a result
of the action reads values from the instance variables of the controller
method and uses those values for rendering the output. When the user
interacts with the view and clicks on something, it will send a request
to a controller armed with relevant parameters (like the values of
fields entered in a form) stored in the params hash - Full Circle! :)
Hope this helps.
Cheers
Mohit.
I'd just like to say that if you're a total beginner, copying and
pasting is not what you should be doing. The best thing to do is to
manually type out the examples as you read them. Why? Because you
WILL make mistakes, and by making them, you must learn how to fix them
to continue. You'll learn a lot more a lot faster if you do this.
Skimming a book and pasting examples isn't a good way to learn. Get
your hands dirty! Making mistakes is a great way to learn :)
Cheers!
OJ
On Mar 27, 11:05 pm, Mohit Sindhwani <mo_m...@onghu.com> wrote:
ActiveRecord does the Object-Relational mapping (ORM) from a database
row to the object. Check out:
http://ar.rubyonrails.com/classes/ActiveRecord/Base.html#M000344 (not
just the anchor, the whole page)
for much more information about ActiveRecord.
Cheers
Mohit.
Then you have @cart = find_cart
this makes a variable with your session because in your find_cart
method you created a new session that stores your products in cart
Then @cart.add_product(product) this calls the add_to_product method
in cart.rb.
Btw. I had the same problems when i was reading that part...Btw. youll
probably have problems with cartitem later to figure out how thigs
work...
I hope i helped a little.
P.S Sorry for my bad english...