small medium large xlarge

Me_pragsmall
20 Aug 2008, 09:39
Jose Antonio Lobato (6 posts)

Hi,

(NOTE: I do not know if this is the right place to ask this question, if not, I apologize)

I’m now at Task D of the depot application, and at this point a very basic question appears. I guess this is a basic RUBY question but I have no answer for it:

Some code:

def add_to_cart begin product = Product.find(params[:id]) …

Question: if add_to_cart has no parameters where is params defined?, how do i get access to id? How is that I get access to the Product table of the database? I guess everything relays on the objects hierarchy.

I know is basic, but now I’m a bit lost.

thanks is advance

Regards

Samr_small_pragsmall
20 Aug 2008, 12:19
Sam Ruby (634 posts)

params is an attribute of ActionController. By subclassing ActionController, your code has access to it. A brief description of classes is contained in section A.4. If you are coming to Ruby from another object-oriented language, it might be helpful to know that params() is a method, but Ruby does not require parenthesis when there are no parameters to pass. Another way in which Ruby differs from some languages is that there is no requirement to write “self.params()” to access such a method (some languages use ‘this’ instead of ‘self’).

:id is an example of a symbol. It is a constant, much like 5 and 17 are. Rails makes extensive use of symbols. As such, there is no need to declare the use. This is covered in section A.2.

Putting it all together, Rails makes available a hash or dictionary of parameters to all controllers. The index or keys to that hash are symbols. If there are no parameters passed, then this hash will be empty.

Me_pragsmall
20 Aug 2008, 12:48
Jose Antonio Lobato (6 posts)

Thank you very much Sam.

I guess I’ll get it all together on the second read of the book.

Regards

Samr_small_pragsmall
20 Aug 2008, 14:42
Sam Ruby (634 posts)

:-)

I lost count on how many reads of the book I’ve done, and I’m still learning new stuff.

:-)

You must be logged in to comment