gpt4 book ai didi

ruby-on-rails - rails 3 : How do I write forms that don't align exactly with the structure of the model?

转载 作者:行者123 更新时间:2023-12-01 02:49:53 25 4
gpt4 key购买 nike

例如,如果我有一个名为 start_time 的时间属性的简单模型。 ,那么直接的表单代码将是...

<%= form_for newObj do |f| %>
<%= render 'shared/error_messages', :object => f.object %>
<div class="field">
<%= f.label :start_time %><br />
<%= f.text_field :start_time %>
</div>
<div class="actions">
<%= f.submit "Submit" %>
</div>
<% end %>

但我想要两个文本字段,一个用于 time 的日期组件。对象和一个用于 time 的时间组件目的。
  • 我将如何构建该表单代码?
  • 在 Controller 中,我想如何组织发送到表单的数据?我想将时间对象的两个组件分开吗?

  • 非常感谢你的智慧!

    最佳答案

    我认为这可能不是您问题的直接答案,但可能会提供一个很好的起点:

    有一个 Rails View 助手 select_datetime它为日期和时间条目(而不是文本字段)生成下拉菜单。参见文档:http://api.rubyonrails.org/classes/ActionView/Helpers/DateHelper.html#method-i-select_datetime

    Ryan Bates 做了几个相关的 railscasts(链接是 asciicasts,应该包含原始视频的链接):

  • http://asciicasts.com/episodes/32-time-in-text-field
    这处理上面提到的下拉菜单很繁琐,而是使用文本字段。这里的代码可以帮助为日期和时间输入做两个单独的文本字段。
  • http://asciicasts.com/episodes/213-calendars
    这讨论了 javascript 日历小部件。
  • 关于ruby-on-rails - rails 3 : How do I write forms that don't align exactly with the structure of the model?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5759249/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com