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