【问题标题】:Rails 4 & Mongoid : date helpersRails 4 & Mongoid:日期助手
【发布时间】:2014-09-21 14:59:36
【问题描述】:

我们刚刚从 rails ActiveRecord 迁移到 mongoid。在我们看来,我们可以使用 helper

<%= f.date_select :date, :use_two_digit_numbers => true, :order => [:day, :month, :year], class: "form-control" %>

但是它不能与 mongoid 一起使用,因为它正在发送参数

date(3i)"=>"21", "date(2i)"=>"9", "date(1i)"=>"2014"

Rails 会抛出一个

Mongoid::Errors::UnknownAttribute
Problem: Attempted to set a value for 'date(3i)' which is not allowed on the model

This question 提到如果不包含模块,mongoid 就无法做到这一点。但显然我的 Rails 应用程序无法识别该模块?

uninitialized constant Mongoid::MultiParameterAttributes

【问题讨论】:

    标签: ruby-on-rails date mongoid


    【解决方案1】:

    Arf,刚刚发现 this question 提到它已在 mongoid 4 中被删除。然后我将使用这个 gem

    https://github.com/netguru/mongoid-sadstory

    但如果你有其他选择......

    【讨论】:

      【解决方案2】:

      在mongoid 5 版本中,multiple Parameter 看起来已经被废弃了。

      现在我在视图中使用 html5 标签,例如 date_field_tag,在控制器操作(创建、更新)中,我在对象保存到数据库之前添加了一个手动操作,例如 @article.write_attribute(:published_on, params[:published_on])

      虽然这是一个丑陋的解决方案,但它确实有效。

      【讨论】:

        猜你喜欢
        • 2011-06-09
        • 2014-02-23
        • 2014-02-07
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-08-11
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多