关于Q1 Q2这两个问题,说实话,我到现在也没有弄清楚到底是怎么一回事情,把代码都仔仔细细的检查了一遍,也没有发现有什么问题,希望懂得朋友能帮助下,谢谢:)
- 关于hashes的使用
1)哈希是一种数据结构,它维护了一个键对象的集合,而且将每一个键值与一个值关联了起来。哈希也被成为映射(map),因为它们将键值映射到值上。哈希有时也被称为关联数组,因为它们将每一个键和值关联在一起,而且可以将其理解成数组,只不过数组的索引可以是任意对象而不仅仅限于整数。
2)符号是不可改变的、功能受限的字符串,编写成以冒号为前缀。
3)当一个方法有多于两三个的的参数时,程序员很难记清楚参数的顺序。一些语言允许为参数值制定对应的参数名,ruby不支持这种句法,不过如果方法使用哈希对象作为参数(或参数之一),可以得到近似的功能。同时,为了更好的支持这种编程风格,如果哈希对象的最后一个参数(或在后面只有一个用&打头的代码块参数),ruby允许省日哈希字面量的大括号。没有大括号的哈希有时称为裸哈希,如果我们使用这种参数,看起来就像是使用有名参数,可以参照各个喜欢的顺序给定各个参数。
参照上述内容,可以理解redirect_t:action=>'show',:id=>product_id 便是将两个元素的散列传递给方法redirect_to了。
- 关于params的使用(from Rails Guides)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | class ClientsController < ActionController::Base # This action uses query string parameters because it gets run # by an HTTP GET request, but this does not make any difference # to the way in which the parameters are accessed. The URL for # this action would look like this in order to list activated # clients: /clients?status=activated def index if params[ :status ] == "activated" @clients = Client.activated else @clients = Client.unactivated end end # This action uses POST parameters. They are most likely coming # from an HTML form which the user has submitted. The URL for # this RESTful request will be "/clients", and the data will be # sent as part of the request body. def create @client = Client. new (params[ :client ]) if @client .save redirect_to @client else # This line overrides the default rendering behavior, which # would have been to render the "create" view. render :action => "new" end end end |
更对内容可参考:http://stackoverflow.com/questions/6885990/rails-params-explained
Fork A Repo
Using Pull Requests
Steven Meng
2013.1.30
- 关于git的使用(update++)
Fork A Repo
Using Pull Requests
- 最后
Steven Meng
2013.1.30