Server Side Swift with Vapor - Part 24: Edit and | Ray Wenderlich

Learn how to reuse templates to edit models and learn how to delete models on the web.


This is a companion discussion topic for the original entry at https://www.raywenderlich.com/4493-server-side-swift-with-vapor/lessons/24

Hi @0xtim, I downloaded the material of this lesson but there is a problem on AcronymCategoryPivot.swift, User.swift and Category.swift. The app doesn’t work.
Thanks in advance.

@michele you’re right! That’s from an old version, I’ll get it updated for you!

1 Like

@michele should be updated and working now!

1 Like

Great, Thank you so much.
Congratulations for the course.

1 Like

@0xtim Why do you use a POST method instead of using the DELETE method for deleting an acronym? What would the use case be for the DELETE method? Thanks.

@dpenny you can’t send a DELETE request from a browser (without using Javascript). DELETE is certainly more appropriate and used in the REST API in a previous video, but you have to work around the browser unfortunately. You can only send GET and POST requests with a browser and POST is better for a ‘destructive’ action as it is easier to protect with things like CSRF. Does that make sense?

Yes, makes perfect sense. I wasn’t thinking about the browser issues. Thanks for clarifying.

I also downloaded it and have this: [ ERROR ] FluentError.parentRequired: This parent relationship could not be resolved (Parent.swift:38)
How to debug this? Everything looks ok?
It looks like in while saving Acronym the parent relationship create the error. Maybe because there are no parents yet? Optional/.isEmpty error?

@yoyohu that looks like a Fluent error that should be fixed now. If you run vapor update you’ll pull down the latest version of Vapor that should have the fix in. Note there are some breaking changes in RC2 that you will need to fix. I haven’t done the re-records at the moment but all the changes required can be found here

Wow! Quick reply! Thanks for the insight! I will try it immediately ! I really learn a lot from your course, my compliments!

1 Like

Getting Errors thrown from here are not handled in the original implementation

func editAcronymHandler(_ req: Request) -> Future<View> {
        return try flatMap(to: View.self, req.parameter(Acronym.self), User.query(on: req).all(), { (acronym, users) in
            let context = EditAcronymContext(title: "Edit Acronym", acronym: acronym, users: users)
            return try req.leaf().render("createAcronym", context)
        })
    }

and had to modify it into this:

func editAcronymHandler(_ req: Request) -> Future<View> {
        return User.query(on: req).all().flatMap(to: View.self) { users in
            return try req.parameter(Acronym.self).flatMap(to: View.self) { acronym in
                let context = EditAcronymContext(title: "Edit Acronym", acronym: acronym, users: users.isEmpty ? nil : users)
                return try req.leaf().render("createAcronym", context)
            }
        }
    }

For some reason, if I do the try first like below, the error won’t go so I just flip it and it worked ha!

return try req.parameter(Acronym.self).flatMap(to: View.self) { acronym in
            return User.query(on: req).all().flatMap(to: View.self) { users in
                let context = EditAcronymContext(title: "Edit Acronym", acronym: acronym, users: users.isEmpty ? nil : users)
                return try req.leaf().render("createAcronym", context)
            }
        }

You need to make the function throwing, so just fun editAcronymHandler(_ req: Request) throws -> Future<View>

Ooops how could i not see that one magic word! i was just lucky on the other side :yum:

1 Like

Why did i get this error No ‘Content-Type’ header is present. when visiting http://localhost:8080/acronyms/2/edit. Please help.

Nevermind i found the culprit, instead of router.post(“acronyms”, Acronym.parameter, “edit”, use: editAcronymPostHandler) I did router.get(“acronyms”, Acronym.parameter, “edit”, use: editAcronymPostHandler)

@0xtim Can you please help with this when you get a chance? Thank you - much appreciated! :]

thanks for your fast response, but i’ve managed to solved it.

1 Like