在《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?( ).

也就是说当没有数据的时候,是get,而有数据的时候则是post.

例如我们在用户登录的时候,首先转到登录界面,这个时候当然还没有数据,那么

request.get?
为true,输入相应的数据,提交为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 
分享到:
评论