Not able to add Items to Database

南笙酒味 提交于 2020-01-24 19:33:09

问题


// This is a continuation of the questions I have asked from a tutorial by Paul Hudson on youtube -

I have tried to add items to a database (see image below) -

When I should click on the "Add" button on the image above, the boxes should become EMPTY (See image below). Though .Quantum Pizza will not be added to the list of .Statin Island Pizza and .Country pizza, because I have not done further coding), but it should be as the image below -

but, the result is as follows -

Now, I am posting the codes -----

configure.swift -

import  Fluent
import FluentSQLite
import Vapor
import Leaf // added

public func configure(_ config: inout Config, _ env: inout Environment, _ services: inout Services) throws {
    // Register routes to the router
    let router = EngineRouter.default()
    try routes(router)
    services.register(router, as: Router.self)

    let leafProvider = LeafProvider()    // added
    try services.register(leafProvider)  // added
    config.prefer(LeafRenderer.self, for: ViewRenderer.self)// added

    let directoryConfig = DirectoryConfig.detect()
    services.register(directoryConfig)
    try services.register(FluentSQLiteProvider())
    var databaseConfig = DatabasesConfig()
    let db = try SQLiteDatabase(storage: .file(path:"\(directoryConfig.workDir)pizza.db"))

    databaseConfig.add(database: db, as: .sqlite)
    services.register(databaseConfig)

    var migrationConfig = MigrationConfig()
    migrationConfig.add(model: Pizza.self, database: .sqlite)
    services.register(migrationConfig)
    let serverConfigure = NIOServerConfig.default(hostname: "0.0.0.0", port: 9090)
    services.register(serverConfigure)
}

routes.swift -

import Routing
import Vapor
import FluentSQLite

public func routes(_ router: Router) throws {
    router.get { req -> Future <View> in
        let Newyorker = Pizza(id: 5, name: "Statin Island Pizza", description: "Impractical Jokers Funny Pizza", price: 55)
        let Traditional = Pizza(id: 5, name: "Country Pizza ", description: "Johny Cash Special", price: 55)

        return try req.view().render("welcome",["pizza":[Newyorker,Traditional]])
    }

    router.post(Pizza.self, at: "add") { req, pizza -> Future<Response> in
        return pizza.save(on:req).map(to:Response.self) { Pizza in
            return req.redirect(to: "/")
        }
    }
}

pizza.swift -

import Foundation
import Vapor
import FluentSQLite

struct Pizza: Encodable, Content, Decodable, SQLiteModel, Migration {
    var id:  Int?
    var name: String
    var description: String
    var price: Int
}

leaf screenshot (I tried to paste code, but couldn't, in the correct format. So adding screeshot) -

Edit 1: screenshot after I click on the Add button -

Ill be happy to provide you any further information if you need. Also, I would like to know if the title of my question should be modfied or anyhing should be added to it. Thank you.


回答1:


Your forms action should be action="add" (you're missing the closing quotation to close the action)




回答2:


based on OxTim's answer - It was. a simple leaf-formatting/inverted-comma issue.

correct leaf formatted -

 <! DOCTYPE html>
 <html>
  <body>
 <h1> Pizza </h1>
 <p> Welcome to best pizza in the Andromeda Galaxy.
  <ul>
  #for(pizza in pizza) {
  <li> #(pizza.name) </li>
}
 </ul>

 <form method="post" action="/add">
<p>Name: <input type="text" name="name" /></p
<p>Description: <input type="text" name="description" /></p>
 <p>Price: <input type="number" name="price" /></p>
 <button type="submit">Add</button>
 </form>


来源:https://stackoverflow.com/questions/59545558/not-able-to-add-items-to-database

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!