如何在haml中获取一个复选框数组? [英] How do I get an array of check boxes in haml?

查看:156
本文介绍了如何在haml中获取一个复选框数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

我使用haml来渲染html。

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?

我看到只是放置

= @theModels

将给出@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.

更新:

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

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:

谢谢!

%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 think you should send the content as an hash instead. This will give you the opportunity to set initial values in the form.

哈希@params会给你结果。

The hash @params will give you the result.

例如{oranges=>1}

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

这篇关于如何在haml中获取一个复选框数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆