如何在Yesod中显示有很多关系的<select>?</select>

时间:2014-06-13 01:33:52

标签: haskell yesod persistent yesod-forms

我有一个简单的一对多关系,比如说

Process
    name Text

Report
    time Text
    process ProcessId

我想创建一个用于创建帖子的表单,我可以从现有用户列表中进行选择。这样的事情。

processOptions :: Handler (OptionList (KeyBackend SqlBackend Process))
processOptions = optionsPersist [] [Desc ProcessName] id

postForm = renderDivs $ Report
             <$> areq textField "Time" Nothing
             <*> areq (selectField processOptions) "Process" Nothing

问题是,我不能弄清楚如何正确使用selectFieldoptionsPersist。我查看了线索的源代码,但我无法弄清楚该怎么做。

optionsPersist的预期类型似乎是Handler (OptionList (KeyBackend SqlBackend Process)),但它实际返回的是Handler (OptionList (Entity Process))。我不确定我是否遗漏了某些东西,或者是否需要进行一些解缠。

这是实际的错误消息

Couldn't match type ‘Entity (ProcessGeneric SqlBackend)’
              with ‘KeyBackend SqlBackend Process’
Expected type: Handler (OptionList (KeyBackend SqlBackend Process))
  Actual type: HandlerT
                 App IO (OptionList (Entity (ProcessGeneric SqlBackend)))

虽然有is a question for a similar topic,但我不认为这是重复的,因为这个问题是关于使用optionsPersist,而另一个问题只是手动生成选项。

1 个答案:

答案 0 :(得分:1)

认为你几乎就在那里。问题似乎与您调用selectFieldprocessOptions的方式无关。问题是该调用的结果将是Entity Process,而Report中的第二个字段是ProcessId。所以你只需要使用fmap(又名<$>)进行转换。我相信以下内容会这样做:

entityKey <$> (areq (selectField processOptions) "Process" Nothing)