Rails学习笔记(6)

接上一篇Rails学习笔记(5),继续rails的学习。

  • 书上遇到的一些问题(Task I)
图片
图片
 
关于上述这两个问题,说实话,我到现在也没有弄清楚到底是怎么一回事情,把代码都仔仔细细的检查了一遍,也没有发现有什么问题,希望懂得朋友能帮助下,谢谢:)

  • 关于hashes的使用
在rails编程过程中,遇到最多的貌似就是这种redirect_t:action=>'show',:id=>product_id 似的代码了,说实话,对这种形式的代码最不感冒了,因为是真正意义上的不理解,特别是那个加:的意思,有时候真的让人搞得一头雾水。虽然我知道redirect_to是一个rails函数,:action=>'show',:id=>product_id 是这个函数的hash参数,而且还可以不加{}(前提是hash是方法调用的最后一个参数),等等。不过为了能更好的理解它,现就该内容做一个小小的梳理(参考《Ruby编程语言》):
1)哈希是一种数据结构,它维护了一个键对象的集合,而且将每一个键值与一个值关联了起来。哈希也被成为映射(map),因为它们将键值映射到值上。哈希有时也被称为关联数组,因为它们将每一个键和值关联在一起,而且可以将其理解成数组,只不过数组的索引可以是任意对象而不仅仅限于整数。                
2)符号是不可改变的、功能受限的字符串,编写成以冒号为前缀。                                   
3)当一个方法有多于两三个的的参数时,程序员很难记清楚参数的顺序。一些语言允许为参数值制定对应的参数名,ruby不支持这种句法,不过如果方法使用哈希对象作为参数(或参数之一),可以得到近似的功能。同时,为了更好的支持这种编程风格,如果哈希对象的最后一个参数(或在后面只有一个用&打头的代码块参数),ruby允许省日哈希字面量的大括号。没有大括号的哈希有时称为裸哈希,如果我们使用这种参数,看起来就像是使用有名参数,可以参照各个喜欢的顺序给定各个参数。
参照上述内容,可以理解redirect_t:action=>'show',:id=>product_id 便是将两个元素的散列传递给方法redirect_to了。

You will probably want to access data sent in by the user or other parameters in your controller actions. There are two kinds of parameters possible in a web application. The first are parameters that are sent as part of the URL, called query string parameters. The query string is everything after “?” in the URL. The second type of parameter is usually referred to as POST data. This information usually comes from an HTML form which has been filled in by the user. It’s called POST data because it can only be sent as part of an HTTP POST request. Rails does not make any distinction between query string parameters and POST parameters, and both are available in the params hash in your controller:
 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
 
  • 最后
depot demo终于按照书上写完了,大概总共花了20多天的时间吧。这个短短的学习过程,对rails的使用说不上有什么真正全方位的了解,感觉还是站在rails的门外(个人感觉)。脑子里还是那种一团浆糊的感觉,不过,也没有气馁什么的,学一样东西本来就需要花时间的,而且是大量的时间,而现在还过了只有20多天呢,不急不急,在慢慢的深入学习吧。

Steven Meng
2013.1.30
posted @ 2013-01-30 16:53  StevenMeng  阅读(302)  评论(0编辑  收藏  举报