• 请求和响应:类ActionController::Base ; 类ActionDispatch::Request


    扩展:ActionController::Base < Metal

    2个基本主题:

    •  Get and Show 
    •  do and redirect 

    Requests

    每个请求,由router决定了controller和action keys。剩下的请求参数,the session, 和所有http headers会通过request accessor方法被制造出来给action,然后action被执行。

    完全的请求对象可以通过请求accessor方法使用。主要用于查询HTTP headers。例如:

    def server_ip
      location = request.env["REMOTE_ADDR"]
      render plain: "This server hosted at #{location}"
    end

    Parameters

    所有请求参数,无论是来自URL中的查询字符串还是表格通过a POST request提交的data, 都可以用params方法返回一个hash。

    例子:一个action被执行,通过/post?category=All&limit=5。 params中就会包括{"category" => "All", "limit" => 5}

    例子:类似表格

    <input type="text" name="post[name]" value="david">
    <input type="text" name="post[address]" value=":">

    提交后会params中包括{"post" => {"name" => "david", "address" => "hyacintvej"}}

    Session

    用于在请求之间储存对象的。使用session方法,通过a hash来储存对象。例子:

    储存:session[:person] = Person.authenticate(user_name, password)

    取出:seesion[:person]

    设置为nil :  session[:person] = nil

    移除: reset_session

    session默认储存在浏览器cookie中。

     

    Response

    每个action都会导致a response, 它会把headers, document发送给用户浏览器。

    实际的请求对象是自动的通过用户的renders和redirects产生的,无需用户干涉。

    Renders

    Action Controller发送内容给用户,通过渲染方法(五种类型中的一种)。

    常见的是渲染template。

    包括在Action Pack中的是Action View, 它能渲染ERB模版。它是自动配置的。

    controller传递实例变量给view。

    Redirects

    用于从一个action移动到另外一个action。

    例子:redirect_to action: 'show', id: @entry.id 或者 redirect_to entry_path(@entry)

    ⚠️

    这是一个 额外的HTTP-level redirection,会让浏览器发送第二个请求(a GET to the show action),

    可不是内部re-routing,在一个request中同时调用create方法然后调用show方法(❌想法)


    实例方法:

    request: 返回一个 ActionDispatch::Request实例来响应当前的请求。

    response:Returns an ActionDispatch::Response that represents the current response.



    ActionDispatch::Request Object

    方法: 

    # get "/articles"
    request.fullpath # => "/articles"
    request.headers["Content-Type"] # => "text/plain"

    parmas() /parameters()返回GET/Post参数在一个hash中。


    ActionDispatch::Response Object

    由controller action生成的HTTP response对象。

    用来检索当前response的状态,或者客制化response。也可以代表一个真正的HTTP response(即 发送给浏览器),或者一个TestResponse

    方法:

    response.status = 404
    response.message # => "Not Found"
    response.content_type = "text/plain"

    ActionDispatch::Integration::RequestHelpers

    get, post , delete put, patch, head方法都是用到了process(:方法, path, **args)

    例子

    process :get, '/author', params: { since: 201501011400 }
     
  • 相关阅读:
    EEPlat 的 后台业务处理模型
    poj 1012 Joseph (约瑟夫问题)
    python使用正則表達式
    二维码_encode与decode
    UITableView显示不全
    Bottle 中文文档
    不相交集python实现
    面试题1:落单的数
    Android开发/源代码资源汇总
    leetCode 26.Remove Duplicates from Sorted Array(删除数组反复点) 解题思路和方法
  • 原文地址:https://www.cnblogs.com/chentianwei/p/9564623.html
Copyright © 2020-2023  润新知