躺在沙灘上的小豬

          快樂的每一天

          Post and Get

          在《Agile Web Development with Rails》中看到如下的一句話

          ???? If it has no associated data, it will come in as a GET request. If instead it contains form data, we’ll see a POST. Inside a Rails controller, the request information is available in the attribute request. We can check the request type using the methods get?( ) and post?( ).

          也就是說當(dāng)沒有數(shù)據(jù)的時(shí)候,是get,而有數(shù)據(jù)的時(shí)候則是post.

          例如我們在用戶登錄的時(shí)候,首先轉(zhuǎn)到登錄界面,這個(gè)時(shí)候當(dāng)然還沒有數(shù)據(jù),那么

          request.get?

          為true,輸入相應(yīng)的數(shù)據(jù),提交為false,這樣我們可以通過一個(gè)action來控制流程
          ?
          ??def?login
          ??????
          if?request.get?
          ??????????session[:user_id]
          =nil
          ??????????@user?
          =?User.new
          ??????
          else
          ??????????@user?
          =?User.new(params[:user])
          ??????????logged_in_user?
          =?@user.try_to_login
          ??????????
          if?logged_in_user
          ??????????????session[:user_id]
          =logged_in_user.id
          ??????????????redirect_to?:action
          =>'index'
          ??????????
          else
          ??????????????flash[:notice]
          ="Invalid?user/password?combination"
          ??????????end
          ??????end
          ??end?

          posted on 2006-05-16 10:08 martin xus 閱讀(267) 評論(0)  編輯  收藏

          主站蜘蛛池模板: 南宫市| 玛曲县| 拜泉县| 云龙县| 巨鹿县| 平陆县| 德庆县| 改则县| 邳州市| 兴仁县| 玛多县| 泽州县| 抚顺市| 勃利县| 铁岭市| 浦江县| 木兰县| 五大连池市| 犍为县| 大邑县| 盐源县| 民县| 蚌埠市| 承德市| 泾川县| 通江县| 团风县| 沁源县| 陇川县| 无锡市| 古浪县| 晋江市| 囊谦县| 宁海县| 弥渡县| 苏尼特右旗| 丹棱县| 九寨沟县| 峨山| 南召县| 蕲春县|