Rails 中的问题,请教

悬赏:5 发布时间:2008-07-07 提问人:zhxylove (初级程序员)

Ruby接触不久,,找了关天,没找到

BlogController < ActionController::Base
def show
@customer = find_customer
end

def update
@customer = find_customer
@customer.attributes = params[:customer]
@customer.save ?
redirect_to(:action => "display") :
render(:action => "edit")
end

private
def find_customer() Customer.find(params[:id]) end
end
这段代码是摘自rails ReadMe中的, 这个params[:customer]中的":"代表啥意思,,没有懂这个:customer是啥意思,,什么时候会用到

采纳的答案

2008-07-07 qichunren (资深程序员)

params[:customer]

它是取页面上的传的值(如表单中的input输入控件或者url的问号后面)
如果在你的页面的表单中有一个<input name="customer" type="text" />
这样的话,你在controller的action中就可以通过params[:customer]来取得input输入框中所输入的值.

另外 : 符号在Ruby中表示一个symbol,就是一个符号而已.

还有一点,在Rails中可以使用嵌套的参数形式,例如在你的页面表单中有
<input id="user_name" name="user[name]" size="30" type="text" /><br/>
<input id="user_name" name="user[pwd]" size="30" type="text" />

这样的话,你在action方法中,只需要params[:user]就能获取user的两个参数name和pwd.
具体一个例子就是
@user = User.new(params[:user])
看懂了吧,它很方便的,如果换成取单个的参数 也是可以了,不过那样要多写好几行的代码.

提问者对于答案的评价:
谢谢你的回答,,有些懂了,