躺在沙灘上的小豬

          快樂的每一天

          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ù)的時候,是get,而有數(shù)據(jù)的時候則是post.

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

          request.get?

          為true,輸入相應(yīng)的數(shù)據(jù),提交為false,這樣我們可以通過一個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 閱讀(265) 評論(0)  編輯  收藏

          主站蜘蛛池模板: 龙井市| 三都| 板桥市| 新泰市| 永平县| 高密市| 韶关市| 宁化县| 平阳县| 西丰县| 澎湖县| 沿河| 高台县| 淳化县| 沅江市| 桂林市| 晋中市| 沙田区| 漯河市| 井陉县| 潮州市| 阳谷县| 彩票| 偃师市| 台江县| 木兰县| 武穴市| 合阳县| 秀山| 涞源县| 武平县| 宁阳县| 齐河县| 余干县| 潼南县| 新郑市| 双鸭山市| 寿阳县| 油尖旺区| 元朗区| 旺苍县|