我正在使用simple_form gem并且非常喜欢它的简单性。但是,尝试设置一个简单的单选按钮超出了我,因为我不断收到以下错误
“未定义的局部变量或方法`vegan'”
1.这是我到目前为止所拥有的
<%= f.collection_radio_buttons :diettype, [[vegan, 'vegan'] ,[vegetarian, 'vegetarian']]%>
2.看看我在simple_form之前使用的代码和更新/补丁方法,它会更新并保存用户需求
<%= f.label(:diettype, "Vegan") %>
<%= f.radio_button(:diettype, "Vegan") %>
<%= f.label(:diettype, "vegetarian") %>
<%= f.radio_button(:diettype, "vegetarian")%>
3.这就是我想要重现的内容
<select>
<option> vegan </option>
<option> Vegetarian </option>
</select>
注意 - 纯素食和素食是选择的选项,将存储在以下数据库列中:diettype。
答案 0 :(得分:9)
在controller action
中,添加此行
def actionname
@types = ModelName.select(:diettype).distinct
..
end
其中,
actionname
是渲染视图的操作。
ModelName
是您的模型的名称,其中包含diettype
字段。
在view
中,替换
<%= f.collection_radio_buttons :diettype, [[vegan, 'vegan'] ,[vegetarian, 'vegetarian']]%>
与
<%= f.collection_radio_buttons :diettype, @types, :diettype, :diettype %>
修改强>
<%= f.collection_radio_buttons :diettype, @types, :diettype, :diettype %>
上述行意味着:
创建一个单选按钮集合,其中,
第一个:diettype
:变量将在您选择单选按钮时设置
@types
:传递此集合
第二个:diettype
:正在选择的值
第3个:diettype
:按钮旁边的显示文字
<强> EDIT2 强>
正如您指定的那样,您需要static collection
并且您没有从数据库中获取任何值:
只需在视图中添加以下行,无需更改控制器:
<%= f.collection_radio_buttons :name, [['vegan', 'vegan'] ,['vegetarian', 'vegetarian']],:first, :last %>
答案 1 :(得分:0)
这是使用简单表单添加单选按钮的最简单解决方案,对我来说非常有用。
Private Async Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim numberOfPages = Await DownloadPdfPagesAsync("http://jixxer.com/123", "D:\dls\title1", "", 3)
If numberOfPages > 0 Then
MessageBox.Show($"Download completed. Number of pages: {numberOfPages}")
Else
MessageBox.Show("Download failed")
End If
End Sub
Private Async Function DownloadPdfPagesAsync(baseAddress As String, filesPath As String, Optional resourceName As String = "", Optional startPage As Integer = 1) As Task(Of Integer)
If String.IsNullOrEmpty(resourceName) Then resourceName = Date.Now.ToString("yyyy-MM-dd")
Dim resourceAddr = Path.Combine(baseAddress, resourceName, "pdf")
Dim pageCount = 0
Dim client = New WebClient()
Try
Do
Dim documentName = $"page{startPage + pageCount}.pdf"
Dim resourceUri = New Uri(Path.Combine(resourceAddr, documentName), UriKind.Absolute)
Dim fileName = Path.Combine(filesPath, documentName)
Await client.DownloadFileTaskAsync(resourceUri, fileName)
pageCount += 1
Loop
Catch ex As WebException
If ex.Response IsNot Nothing Then
Dim statusCode = DirectCast(ex.Response, HttpWebResponse).StatusCode
If statusCode = HttpStatusCode.NotFound Then
Return pageCount
End If
ElseIf ex.Status = WebExceptionStatus.ProtocolError AndAlso ex.Message.Contains("404") Then
Return pageCount
Else
' Log and/or ...
Throw
End If
Return 0
Finally
client.Dispose()
End Try
End Function