I have an array of strings, called @theModels, in a routine implemented as part of a Sinatra server. These models are options for the user to select, and are obtained by the back end (the idea being, as new models are added, then the front end code should not change).
我在作为Sinatra服务器的一部分实现的例程中有一个名为@theModels的字符串数组。这些模型是供用户选择的选项,由后端获取(想法是,随着新模型的添加,前端代码不应更改)。
I'm using haml to render html.
我正在使用haml来渲染html。
How can I enumerate each element in the list of @theModels such that each element is a checkbox? And how can I obtain which checkboxes the user has selected?
如何枚举@theModels列表中的每个元素,使每个元素都是一个复选框?如何获取用户选择的复选框?
I see that just putting
我看到只是放
= @theModels
will give me the list of strings contained in @theModels, but without spacing or the like, and certainly not in checkboxes. I've found this question that appears to be similar, but my haml-fu isn't good enough to convert that into what I need.
将给我@theModels中包含的字符串列表,但没有间距等,当然也不在复选框中。我发现这个问题似乎很相似,但我的haml-fu还不够好,无法将其转化为我需要的东西。
UPDATE:
These are options associated with a file upload, such that now the code looks like:
这些是与文件上传相关的选项,现在代码如下所示:
%form{:action=>"/Upload",:method=>"post",:enctype=>"multipart/form-data"}
- @theModelHash.each do |key,value|
%br
%input{:type=>"checkbox", :name=>"#{key}", :value=>1, :checked=>value}
=key
%input{:type=>"file",:name=>"file"}
%input{:type=>"submit",:value=>"Upload"}
Problem is, that puts a file upload button on each option, instead of at the end. I only want one submit button in the end; should I have two forms that both report their results when the 'Upload' button is pressed?
问题是,在每个选项上放置文件上传按钮,而不是在最后。我最后只想要一个提交按钮;我应该有两个表单,当按下“上传”按钮时,它们都会报告结果吗?
UPDATE2:
After a moment's thought, the above can be modified to:
经过一段时间的思考,上面的内容可以修改为:
Thanks!
%form{:action=>"/Upload",:method=>"post",:enctype=>"multipart/form-data"}
- @theModelHash.each do |key,value|
%br
%input{:type=>"checkbox", :name=>"#{key}", :value=>1, :checked=>value}
=key
%form{:action=>"/Upload",:method=>"post",:enctype=>"multipart/form-data"}
%input{:type=>"file",:name=>"file"}
%input{:type=>"submit",:value=>"Upload"}
And that appears to do what I want.
这似乎做我想要的。
2 个解决方案
#1
3
I think you should send the content as an hash instead. This will give you the opportunity to set initial values in the form.
我认为你应该把内容作为哈希发送。这将使您有机会在表单中设置初始值。
The hash @params will give you the result.
哈希@params将为您提供结果。
E.g. {"oranges"=>"1"}
#app.haml %form{:method => 'post', :action => "/"} - @models.each do |key,value| %br %input{:type=>"checkbox", :name=>"#{key}", :value=>1, :checked=>value} =key %input{:type => :submit, :value => "Save"} #app.rb require 'sinatra' require 'haml' get '/' do @models = {"oranges" => true, "bananas" => false} haml :app end post '/' do @params.inspect end
#2
0
The link you provided linked to a rails solution where you have a function returning the proper html.
您提供的链接链接到rails解决方案,其中您有一个函数返回正确的html。
You can define this function yourself:
您可以自己定义此功能:
Input: key, value Output: %input{:type=>"checkbox", :name=>"#{key}", :value=>1, :checked=>value} def check_box(key, value) ... end and call it in haml with =check_box(key,value)
#1
3
I think you should send the content as an hash instead. This will give you the opportunity to set initial values in the form.
我认为你应该把内容作为哈希发送。这将使您有机会在表单中设置初始值。
The hash @params will give you the result.
哈希@params将为您提供结果。
E.g. {"oranges"=>"1"}
#app.haml %form{:method => 'post', :action => "/"} - @models.each do |key,value| %br %input{:type=>"checkbox", :name=>"#{key}", :value=>1, :checked=>value} =key %input{:type => :submit, :value => "Save"} #app.rb require 'sinatra' require 'haml' get '/' do @models = {"oranges" => true, "bananas" => false} haml :app end post '/' do @params.inspect end
#2
0
The link you provided linked to a rails solution where you have a function returning the proper html.
您提供的链接链接到rails解决方案,其中您有一个函数返回正确的html。
You can define this function yourself:
您可以自己定义此功能:
Input: key, value Output: %input{:type=>"checkbox", :name=>"#{key}", :value=>1, :checked=>value} def check_box(key, value) ... end and call it in haml with =check_box(key,value)