`NewView`的字段未初始化。

huangapple go评论50阅读模式
英文:

Fields of `NewView' not initialised

问题

I'm trying to go through the ihp blog tutorial, but I'm not sure how to resolve this error. I'm new to haskell. I've tried asking chat gpt, but no luck so far.

Do I need to initialize an empty comment when I create a post? I assume so because NewView is defined with that field.

error

Web/Controller/Posts.hs:47:37
    * Fields of `NewView' not initialised:
        comment :: Comment
    * In the first argument of `render', namely `NewView {..}'
      In the expression: render NewView {..}
      In a case alternative: Left post -> render NewView {..}
   |
47 |                 Left post -> render NewView { .. }
   |                                     ^^^^^^^^^^^^^^

source code

-- Web/Controller/Posts.hs
action CreatePostAction = do
    let post = newRecord @Post
    post
        |> buildPost
        |> ifValid \case
            Left post -> render NewView { .. }
            Right post -> do
                post <- post |> createRecord
                setSuccessMessage "Post created"
                redirectTo PostsAction
-- ./Web/View/Comments/New.hs
data NewView = NewView
    { comment :: Comment
    , post :: Post
    }
英文:

I'm trying to go through the ihp blog tutorial, but I'm not sure how to resolve this error. I'm new to haskell. I've tried asking chat gpt, but no luck so far.

Do I need to initialize an empty comment when I create a post? I assume so because NewView is defined with that field.

error

Web/Controller/Posts.hs:47:37
    * Fields of `NewView' not initialised:
        comment :: Comment
    * In the first argument of `render', namely `NewView {..}'
      In the expression: render NewView {..}
      In a case alternative: Left post -> render NewView {..}
   |
47 |                 Left post -> render NewView { .. }
   |                                     ^^^^^^^^^^^^^^

source code

-- Web/Controller/Posts.hs
action CreatePostAction = do
    let post = newRecord @Post
    post
        |> buildPost
        |> ifValid \case
            Left post -> render NewView { .. }
            Right post -> do
                post <- post |> createRecord
                setSuccessMessage "Post created"
                redirectTo PostsAction
-- ./Web/View/Comments/New.hs
data NewView = NewView
    { comment :: Comment
    , post :: Post
    }

答案1

得分: 2

错误发生是因为视图需要从控制器操作中传递comment变量。

你很可能已经在NewPostAction中设置了comment变量,类似于这样:

action NewPostAction = do
    let post = newRecord @Post
    let comment = newRecord @Comment
    render NewView { .. }

当新提交的帖子验证失败时,CreatePostAction也会渲染NewView,以显示验证错误消息。所以你还需要在CreatePostActionLeft分支中设置let comment = newRecord @Comment

-- Web/Controller/Posts.hs
action CreatePostAction = do
    let post = newRecord @Post
    post
        |> buildPost
        |> ifValid \case
            Left post -> do
                let comment = newRecord @Comment -- <--- 这是新的
                render NewView { .. }
            Right post -> do
                post <- post |> createRecord
                setSuccessMessage "Post created"
                redirectTo PostsAction
英文:

The error happens as the view requires the comment variable to be passed from the controller action.

You've likely already set the comment variable inside your NewPostAction, something like this:

action NewPostAction = do
    let post = newRecord @Post
    let comment = newRecord @Comment
    render NewView { .. }

The CreatePostAction is also rendering the NewView when the validation of the newly submitted post has failed, to display the validation error messages. So you also need to set the let comment = newRecord @Comment inside the Left branch of the CreatePostAction:

-- Web/Controller/Posts.hs
action CreatePostAction = do
    let post = newRecord @Post
    post
        |> buildPost
        |> ifValid \case
            Left post -> do
                let comment = newRecord @Comment -- <--- THIS IS NEW
                render NewView { .. }
            Right post -> do
                post <- post |> createRecord
                setSuccessMessage "Post created"
                redirectTo PostsAction

答案2

得分: 0

我在帖子的NewView中添加了一个注释字段。那是个错误。移除那个字段修复了问题:

英文:

I added a comment field to a post's NewView. That was a mistake. Removing that field fixed things:

diff --git a/Web/View/Posts/New.hs b/Web/View/Posts/New.hs
index 79b073f..9cd06d0 100644
--- a/Web/View/Posts/New.hs
+++ b/Web/View/Posts/New.hs
@@ -3,8 +3,7 @@ import Web.View.Prelude
 import qualified Text.MMark as MMark
 
 data NewView = NewView
-    { comment :: Comment
-    , post :: Post
+    { post :: Post
     }
 
 instance View NewView where

huangapple
  • 本文由 发表于 2023年2月27日 12:50:09
  • 转载请务必保留本文链接:https://go.coder-hub.com/75576861.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定