如何使用POST /表单叶子模板传递数据?

时间:2018-04-01 01:34:14

标签: swift post vapor leaf

我对steam / leaf / html的理解有两个主要差距。我正在使用" todo"使用蒸汽的β分支创建的示例。

首先,我制作了自己的流畅模型(没有我知道的问题):

import FluentSQLite
import Vapor
final class affordatmodel: SQLiteModel {
    var id: Int?
    var propertyCost: String
    var targetEquity: String
    var interestRate: String
    var amortization: String
    var sponsor1: String
    var sponsor2: String
    var rent: String
    var rentInflation: String
    var propertyTaxes: String
    var propertyTaxesInflation: String
    var strataFees: String
    var strataFeesInflation: String
    init(propertyCost: String, targetEquity: String, interestRate: String, amortization: String, sponsor1: String, sponsor2: String, rent: String, rentInflation: String, propertyTaxes: String, propertyTaxesInflation: String, strataFees: String, strataFeesInflation: String) {
        self.propertyCost = propertyCost
        self.targetEquity = targetEquity
        self.interestRate = interestRate
        self.amortization = amortization
        self.sponsor1 = sponsor1
        self.sponsor2 = sponsor2
        self.rent = rent
        self.rentInflation = rentInflation
        self.propertyTaxes = propertyTaxes
        self.propertyTaxesInflation = propertyTaxesInflation
        self.strataFees = strataFees
        self.strataFeesInflation = strataFeesInflation
    }
}
/// Allows to be used as a dynamic migration.
extension affordatmodel: Migration { }
/// Allows to be encoded to and decoded from HTTP messages.
extension affordatmodel: Content { }
/// Allows to be used as a dynamic parameter in route definitions.
extension affordatmodel: Parameter { }

然后我创建一个实例并将其发送到叶子模板:

let defaultData = affordatmodel(propertyCost: "700000", targetEquity: "300000", interestRate: "1", amortization: "20", sponsor1: "500000", sponsor2: "200000", rent: "1200", rentInflation: "1", propertyTaxes: "8000", propertyTaxesInflation: "1", strataFees: "0", strataFeesInflation: "0")
return leaf.render("welcome", ["affordat": defaultData])

我的Leaf模板使用默认数据(此处显示的主体)成功填充了html:

<body class="container">
    <h1>Payment and Principal Calculations</h1>

    <form action="/affordat" method="POST">
        <div class="form-group">
            <label for="propertyCost">Property Cost</label>
            <input type="number" class="form-control" name="propertyCost" placeholder="#(affordat.propertyCost)">
                </div>
        <div class="form-group">
            <label for="targetEquity">Target Equity</label>
            <input type="number" class="form-control" name="targetEquity" placeholder="#(affordat.targetEquity)">
                </div>
        <div class="form-group">
            <label for="interestRate">Interest Rate</label>
            <input type="number" class="form-control" name="interestRate" placeholder="#(affordat.interestRate)">
                </div>
        <div class="form-group">
            <label for="amortization">Amortization (years)</label>
            <input type="number" class="form-control" name="amortization" placeholder="#(affordat.amortization)">
                </div>
        <div class="form-group">
            <label for="sponsor1">Sponsor 1 Funds</label>
            <input type="number" class="form-control" name="sponsor1" placeholder="#(affordat.sponsor1)">
                </div>
        <div class="form-group">
            <label for="sponsor2">Sponsor 2 Funds</label>
            <input type="number" class="form-control" name="sponsor2" placeholder="#(affordat.sponsor2)">
                </div>
        <div class="form-group">
            <label for="rent">Rent</label>
            <input type="number" class="form-control" name="rent" placeholder="#(affordat.rent)">
                </div>
        <div class="form-group">
            <label for="rentInflation">Rent Inflation (will be used exactly)</label>
            <input type="number" class="form-control" name="rentInflation" placeholder="#(affordat.rentInflation)">
                </div>
        <div class="form-group">
            <label for="propertyTaxes">Property Taxes (first year est.)</label>
            <input type="number" class="form-control" name="propertyTaxes" placeholder="#(affordat.propertyTaxes)">
                </div>
        <div class="form-group">
            <label for="propertyTaxesInflation">Property Taxes Inflation (est.)</label>
            <input type="number" class="form-control" name="propertyTaxesInflation" placeholder="#(affordat.propertyTaxesInflation)">
                </div>
        <div class="form-group">
            <label for="strataFees">Strata Fees (first year est.)</label>
            <input type="number" class="form-control" name="strataFees" placeholder="#(affordat.strataFees)">
                </div>
        <div class="form-group">
            <label for="strataFeesInflation">Strata Fees Inflation (est.)</label>
            <input type="number" class="form-control" name="strataFeesInflation" placeholder="#(affordat.strataFeesInflation)">
                </div>

        <input type="hidden" name="_csrf" value="{{csrfToken}}">
            <button type="submit" class="btn btn-primary">Refresh Calculations</button>
            </form>

</body>

很好,所以我知道如何将流畅的数据传输到HTML。我的问题是我不知道怎么回来。当&#34; Post&#34;发生时,数据似乎没有传递给控制器​​。我的路线是:

router.post("affordat", use: affordatController.create)

我的控制器的相关部分看起来像这样:

import Vapor
final class AffordatController {
    func create(_ req: Request) throws -> Future<affordatmodel> {
        return try req.content.decode(affordatmodel.self).flatMap(to: affordatmodel.self) { affordatmodel1 in
            return affordatmodel1.save(on: req)
        }
    }
}

这显示了我的一个模型,ID#,但没有数据。我有点理解为什么,因为我似乎并没有真正将发布数据发送给控制器。 我应该如何将POST数据发送到控制器?我的叶子模板,路线或控制器中的问题是什么?

1 个答案:

答案 0 :(得分:0)

看起来应该可以。您可以在浏览器的网络检查器中检查发送到服务器的数据。确保您保留日志,并且能够看到POST请求和发送到服务器的数据。

如果在请求中的每个点都有断点,则可以看到数据是什么。

顺便说一句,您似乎正在提交一个空表格,因此它只是将所有内容填充为空白字符串。您是要在表单输入中使用concatMap而不是this.images$ = this.queue.pipe( concatMap(event => event), // I assume the event here is something like this.streamA$ tap(data => // do stuff with data) ); 吗?值将为用户预先填充数据,占位符将向用户显示该值作为建议,但不会在表单提交中发送数据。