简体   繁体   English

如何在Ruby on Rails上创建选择表单?

[英]How do I create a selection form on Ruby on Rails?

I am trying to create a form that displays multiple options of how to display data on a map based on different spans of time (day of week, time of day...). 我正在尝试创建一个表单,该表单根据不同的时间跨度(星期几,一天中的时间...)显示如何在地图上显示数据的多个选项。 I would like to save the options the user inputs from the form using Ruby on Rails but am unaware of how to create a form using the select tag. 我想保存用户使用Ruby on Rails从表单输入的选项,但是不知道如何使用select标签创建表单。 I have the code in html as of now but would like to convert it into Ruby on Rails so I can abstract the values inputed by the user easily in order to display the correct data. 到目前为止,我已经有了html中的代码,但想将其转换为Ruby on Rails,因此我可以轻松地抽象出用户输入的值,以显示正确的数据。 This is the code I have 这是我的代码

    <!-- SPAN OF TIME--> 
                         <strong>Span:</strong>
                         <select id="span" class="span2">
                                <option>Today</option>
                                <option>This Week</option>
                                <option>This Month</option>
                                <option>This Year</option>
                            </select>
                            </br> 



                        <!-- WEEKDAY --> 
                        <strong>Weekday:</strong>
                            <select id="weekday" class="span2">
                                <option>All days</option>
                                <option>Weekend</option>
                                <option>Weekdays</option>
                                <option>Sunday</option>
                                <option>Monday</option>
                                <option>Tuesday</option>
                                <option>Wednesday</option>
                                <option>Thursday</option>
                                <option>Friday</option>
                                <option>Saturday</option>
                            </select>
                            </br> 

                        <!-- WEEKDAY --> 
                        <strong>Time of Day:</strong>
                            <select id="time" class="span2">
                                <option>Morning</option>
                                <option>Afternoon</option>
                                <option>Night</option>
                            </select>
                        </br> 


                        <button type="submit" class="btn btn-primary">Show Data</button>

Your option tags need to have a value attribute. 您的选项标签必须具有value属性。 The option value is what is submitted to the controller. 选项值是提交给控制器的值。 You will then be able to access this data through the params hash. 然后,您将能够通过params哈希访问此数据。 I would also suggest using rails form helpers to generate these tags dynamically. 我还建议使用Rails表单帮助程序动态生成这些标签。

http://guides.rubyonrails.org/form_helpers.html#the-select-and-option-tags http://guides.rubyonrails.org/form_helpers.html#the-select-and-option-tags

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM