如何处理非模型Rails表单中的日期字段? [英] How to handle date fields in a non-model Rails form?

查看:216
本文介绍了如何处理非模型Rails表单中的日期字段?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我通过使用 ActiveModel 创建一个非模型对象,该对象将与Rails表单构建器一起使用。这是一个Rails 3项目。以下是我到目前为止的一个例子:

  class SalesReport 
包括ActiveModel :: Validations
include ActiveModel :: Conversion
扩展ActiveModel ::命名

attr_accessor:promotion_code,:start_date,:end_date
$ b def初始化(attributes = {})
attributes.each do | name,value |
send(#{name} =,value)
结束
结束

def持续?
false
end
end

我碰巧使用HAML和simple_form,但这并不重要。最终,我只是使用标准的Rails日期选择字段:

  = simple_form_for [:admin,@report],如:: report,url:admin_reports_path do | f | 
= f.input:promotion_code,label:'Promo Code'
= f.input:start_date,as :: date
= f.input:end_date,as :: date:
= f.button:submit

Rails将日期字段分割成单独的字段,因此当表单被提交,实际上有3个日期字段被提交:

  {
report=> {
start_date(1i)=> 2014,
start_date(2i)=> 4,
start_date(3i)=> 1
}
}

在我的 SalesReport 对象,我将params分配给我的 attr 方法,但是我得到一个错误,我没有 start_date(1i)= 方法,这显然没有定义。最终,我想结束使用 Date 对象,而不是3个单独的字段。



我应该如何在非模型对象中处理这些日期字段?在您的初始化过程中,您可以手动分配值属性到类方法并在下面覆盖你 start_date end_date setter方法。

  class SalesReport 
包含ActiveModel ::验证
包含ActiveModel ::转换
扩展ActiveModel ::命名

attr_accessor:promotion_code,:start_date,:end_date

def initialize(attributes = {})
@promotion_code = attributes ['promotion_code']
year = attributes ['' start_date(1i)']
month = attributes ['start_date(2i)']
day = attributes ['start_date(3i)']
self.start_date = [year,month,day ]
结束

def start_date =(价值e)
如果value.is_a?(Array)
@start_date = Date.new(value [0] .to_i,value [1] .to_i,value [2] .to_i)
否则
@start_date =值
结束
结束

def坚持?
false
end
end

这应该让你给setter是一个 Date 实例或一个 Array 与单独的日期元素和setter将分配正确的日期到 @start_date
只要对 @end_date 执行相同的操作。



希望这能帮助你。


I am creating a non-model object that will be used with a Rails form builder by using ActiveModel. This is a Rails 3 project. Here's an example of what I have so far:

class SalesReport
  include ActiveModel::Validations
  include ActiveModel::Conversion
  extend ActiveModel::Naming

  attr_accessor :promotion_code, :start_date, :end_date

  def initialize(attributes = {})
    attributes.each do |name, value|
      send("#{name}=", value)
    end
  end

  def persisted?
    false
  end
end

I happen to be using HAML and simple_form, but that's not important. Ultimately, I'm just using standard Rails date select fields:

= simple_form_for [:admin, @report], as: :report, url: admin_reports_path do |f|
  = f.input :promotion_code, label: 'Promo Code'
  = f.input :start_date, as: :date
  = f.input :end_date, as: :date
  = f.button :submit

Rails splits up the date fields into individual fields, so when the form is submitted, there are actually 3 date fields that are submitted:

{
  "report" => {
    "start_date(1i)" => "2014",
    "start_date(2i)" => "4",
    "start_date(3i)" => "1"
  }
}

In my SalesReport object, I'm assigning the params to my attr methods, but I'm getting an error that I don't have a start_date(1i)= method, which I obviously haven't defined. Ultimately, I'd like to end up with a Date object that I can use instead of 3 separate fields.

How should I handle these date fields in my non-model object?

解决方案

In your initialization you could manually assign the values from attributes to the class methods and down below override you start_date and end_date setter methods.

class SalesReport
  include ActiveModel::Validations
  include ActiveModel::Conversion
  extend ActiveModel::Naming

  attr_accessor :promotion_code, :start_date, :end_date

  def initialize(attributes = {})
    @promotion_code = attributes['promotion_code']
    year = attributes['start_date(1i)']
    month = attributes['start_date(2i)']
    day = attributes['start_date(3i)']
    self.start_date = [year, month, day]
  end

  def start_date=(value)
    if value.is_a?(Array)
      @start_date = Date.new(value[0].to_i, value[1].to_i, value[2].to_i)
    else
      @start_date = value
    end
  end

  def persisted?
    false
  end
end   

This should allow you to give the setter a Date instance or an Array with the separate date elements and the setter will assign the correct date to @start_date. Just do the same for @end_date.

Hope this can help you.

这篇关于如何处理非模型Rails表单中的日期字段?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆