rails点滴

1.The default HTTP method for a link is a get, and the default HTTP method for a button is a post, and rails uses these conventions to determine which method to call

对于默认http方法,link是get,button是post方式,rails就用这些规定去决定调用哪个方法(index, create)

2.redirect_to(@line_item.cart)的解释,为什么跳转到show页面

It’s a combination of the Rails methods redirect_to, url_for and polymorphic_url (which used to be part of the simply_helpful plugin). When you give redirect_to an object it calls 
redirect_to_full_url(url_for(options), status)
because the object isn’t an explicit URL, a String, or the :back symbol. url_for, when given an object, calls
polymorphic_url(options)
polymorphic_url is a really nifty helper method that returns the appropriate route based on the object’s class. Also, if it’s an ActiveRecord object, it returns a different URL depending on whether the object is a new record or not. So in the redirect_to(@product) from the create method, polymorphic_url sees that it has an instance of the Product class, so it’s either going to return products_path (the index action) or product_path(@product) (the show action). Since this is a new record, it returns the latter, redirecting you to the show page of your newly created product.

2.常用操作命令
单独创建一个controller中内容为空,测试也为空
rails g controller UserSessions 【new】,带着new方法也生成

3.调试方式:(前台页面添加指令显示数据)
1.<% debug(assigns)%> 显示前台页面可以调用的变量值
2.<% debug(headers)%> 显示请求的头部信息
3.<% debug(params)%> 显示请求的参数
4.还有base_path,controller,flash, request, respond, session

4.数据查询:
1.find by id
传入单个id;
传入id数组 find(id1, id2, id3)
传入id区域 find([id1, id3])
2.find all
find(:all) 简写.all, 如User.all
3.find first
find(:first) 或简写.first 如User.first
也可以通过:order参数指定排序规则
4.find last
find(:last) User.last
5.:conditions
find(:all, :conditions => "registered=true")
或者
find(:all, :conditions => {:registered=true})
或者
find(:all, :conditions => ["email = ?", email])
6. 根据列查询
find_all_by_givenCol(name)
find_by_givenCol(name) 只返回第一条
7. :order
默认情况会按升序排序
find(:all, :order => "family_name, given_name") 升序
find(:all, :order => "family_name DESC, given_name DESC")降序
8. :limit
find(:all, :limit => 10) 返回前10条
9. :offset
略过前n条
find(:all, :limit => 10, :offset => 10) 略过前10条取10条记录
10. :readonly
设置返回记录为只读
11. :group
分组
12.:lock, :joins , :include, :select, :from



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值