我有一个 User
型号和 App
我的应用程序中的模型。 App
有一个 belongs_to
与 User
的关系模型。
在模板apps/new.plush.html
我需要将用户列表呈现为下拉选择。我已经实现了forms.Selectable
User
中的如下界面模型 -
func (a *User) SelectLabel() string {
return a.Name
}
func (a *User) SelectValue() interface{} {
return a.ID
}
New()
apps.go
中的操作看起来像这样 -func (v AppsResource) New(c buffalo.Context) error {
tx, ok := c.Value("tx").(*pop.Connection)
if !ok {
return fmt.Errorf("no transaction found")
}
users := &models.Users{}
if atErr := tx.All(users); atErr != nil {
return c.Error(http.StatusNotFound, atErr)
}
c.Set("users", users)
c.Set("app", &models.App{})
return c.Render(http.StatusOK, r.HTML("/apps/new.plush.html"))
}
现在,我如何编写选择标签来呈现 users
中的选项大批?以下不起作用 -
<%= f.SelectTag("UserID", {options: users})%>
最佳答案
我从#buffalo slack channel 找到了解决方案。问题在于 -
func (a *User) SelectLabel() string {
return a.Name
}
func (a *User) SelectValue() interface{} {
return a.ID
}
这些不应该是指针方法。正确的版本是——func (a User) SelectLabel() string {
return a.Name
}
func (a User) SelectValue() interface{} {
return a.ID
}
关于go - 使用模型数组中的选项渲染 SelectTag(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62895236/