如何在 haml 中获取复选框数组?

发布于 2024-08-19 05:16:19 字数 1409 浏览 1 评论 0原文

我有一个字符串数组,称为 @theModels,在作为 Sinatra 服务器的一部分实现的例程中。这些模型是供用户选择的选项,由后端获取(想法是,随着新模型的添加,那么前端代码不应该改变)。

我正在使用 haml 来渲染 html。

如何枚举 @theModels 列表中的每个元素,以便每个元素都是一个复选框?我怎样才能获取用户选择了哪些复选框?

我发现只需放置

= @theModels

就会给我包含在 @theModels 中的字符串列表,但没有空格等,当然也不在复选框中。我发现这个问题看起来很相似,但我的haml-fu 不足以将其转换为我需要的内容。

更新:

这些是与文件上传相关的选项,现在代码如下所示:

%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"}

问题是,在每个选项上放置一个文件上传按钮,而不是在末尾。我最后只想要一个提交按钮;我是否应该有两个表格,在按下“上传”按钮时都报告其结果?

UPDATE2:

想了想,上面可以修改为:

谢谢!

%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"}

这似乎就是我想要的。

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).

I'm using haml to render 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?

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.

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

放低过去 2024-08-26 05:16:19

我认为你应该将内容作为哈希发送。
这将使您有机会在表单中设置初始值。

哈希@params会给你结果。

例如{“橙子”=>“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

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.

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

淡墨 2024-08-26 05:16:19

您提供的链接链接到一个 Rails 解决方案,其中您有一个返回正确 html 的函数。

你可以自己定义这个函数:

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)

The link you provided linked to a rails solution where you have a function returning the proper 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)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文