Declare a controller method as a helper. For example,
helper_method :link_to
def link_to(name, options) … end
makes the link_to controller method available in the view.
课程遇到的如current_cart,current_user. 声明后,就可以在view里面使用这个method了。
find_by,find_by_id,session[]
find_by: 属于ActiveRecord提供的finder methods 之一。通过传递argument来在database中查找。
The find_by method finds the first record matching some conditions.
find_by!: 和find_by一样,但nill的话,会报错!ActiveRecord::RecordNoFound
find_by_id: find_by_id(params[:id]) ,估计这个用法不再使用了。
session[]: 见http://guides.rubyonrails.org/action_controller_overview.html
简单说:就是储存的一小块数据,再controller和view中使用。 guide中篇幅很长,估计至少看30分钟。
http://guides.rubyonrails.org/active_record_querying.html 有22以上中finder methods可用。
(Finder methods that return a collection,such as where and group, return an instance of ActiveRecord::Relation. Methods that find a single entity实体,such as find and first, return a single instance of the model)