【问题标题】:How to work with date_select form helper without a model in Rails?如何在 Rails 中使用没有模型的 date_select 表单助手?
【发布时间】:2023-03-05 07:06:01
【问题描述】:

我有一个变量 @the_date 和一个没有关联模型的 date_select 表单助手。

如何使用date_select显示相应的HTML?

以下不起作用:

<%= date_select "the_date", @the_date %>

【问题讨论】:

    标签: html ruby-on-rails ruby-on-rails-3 form-helpers


    【解决方案1】:

    你可以这样做:

    <% @the_date = Time.now %>
    <%= date_select("my_date", "my_date", :default => @the_date) %>
    

    【讨论】:

    • 这帮助我找到了有效的方法。谢谢!!!! &lt;%= date_select("the_date", "", :default =&gt; @the_date ) %&gt;
    • De nada,你需要帮助器中的虚假“object_name”和“attribute”选项。
    【解决方案2】:

    这就是最终奏效的方法:

    <% @the_date = Date.strptime(@the_date_string, "%Y-%m-%d") %>
    <%= date_select("the_date_string", "", :default => @the_date) %>
    

    我将日期存储为字符串。因此,需要将其转换为日期对象才能显示在 HTML 表单中,并转换回字符串才能保存到数据库中。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-08-17
      • 2011-04-07
      • 1970-01-01
      • 2011-05-07
      • 1970-01-01
      • 2022-01-23
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多