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

标签 ruby-on-rails ruby-on-rails-3

例如,如果我有一个名为 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/

    相关文章:

    ruby - 修改正则表达式以验证密码至少有一个大写字母。 ruby

    mysql - Heroku 数据库 :push is not working

    ruby - Net :Http and third-party library for making API calls in Rails/Ruby?有什么区别

    ruby-on-rails - RoR 缓存如何工作?

    ruby-on-rails - Rails+resque 后台作业导入不向数据库添加任何内容

    html - link_to_unless_current 将类分配给禁用的(当前)链接

    mysql - 使用 parent_id 求和并分组

    ruby-on-rails - 将记录批量插入 Active Record 表

    ruby-on-rails - 在 Rails 中以可测试的方式路由之前,如何修改请求对象?

    ruby-on-rails - 错误 : undefined local variable or method `resource_class' when installing omniauth-twitter