accessing :id
Jose Antonio...
2 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: I know is basic, but now I’m a bit lost. thanks is advance Regards |
Sam Ruby
197 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. |
Jose Antonio...
2 posts
|
Thank you very much Sam. I guess I’ll get it all together on the second read of the book. Regards |
Sam Ruby
197 posts
|
:-) I lost count on how many reads of the book I’ve done, and I’m still learning new stuff. :-) |
4 posts, 2 voices
