English 中文(简体)
铁路多个领域属于一种模式
原标题:Rails multiple fields to one model attribute

我在任何地方都看着,但似乎无法找到一个好的解决办法。

My form has a date (textfield with datepicker) and a time (textfield with timepicker), which I want to map to an model field called due_at.

迄今为止,我一直在我的控制人员中处理这一模式,有不同的参数,可以参加,直到有一天,才能人工确定模式,但是,它令人感到迷惑,而且确实认为这一逻辑应当保留在模型/概览中。

I would like to be able to handle the two form fields to an attribute in the model, then split it back out for errors, edit action etc. Basically a custom way of performing what the standard datetime_select does, but putting my own touch to it.

Is there something that I can put in my model like ?

def due_at=(date, time)
...
end

I been looking a number of places, but can t find out how you would do this. People say to use javascript to populate a hidden field, but just don t seem like the cleanest solution for a pretty simple problem.

Any advice/help would be much appreciated.

Thanks.

最佳回答

首先,请重新命名为你的领域,因为创造“可能与“积极记录”发生冲突。

I did exactly this for a field with the format M/D/YYYY H:M (Hours/Minutes in 24hrs format)

In your model:

attr_accessor :due_date, :due_time

before_validation :make_due_at


def make_due_at
  if @due_date.present? && @due_time.present?
    self.due_at = DateTime.new(@due_date.year, @due_date.month, @due_date.day, @due_time.hour, @due_time.min)
  end
end


def due_date
  return @due_date if @due_date.present?
  return @due_at if @due_at.present?
  return Date.today
end

def due_time
  return @due_time if @due_time.present?
  return @due_at if @due_at.present?
  return Time.now
end 


def due_date=(new_date)
  @due_date = self.string_to_datetime(new_date, I18n.t( date.formats.default ))
end


def due_time=(new_time)
  @due_time = self.string_to_datetime(new_time, I18n.t( time.formats.time ))
end

protected

def string_to_datetime(value, format)
  return value unless value.is_a?(String)

  begin
    DateTime.strptime(value, format)
  rescue ArgumentError
    nil
  end
end

now in the view:

<%= text_field_tag :due_time, I18n.l(@mymodel.due_time, :format => :time) %>
<%= text_field_tag :due_date, I18n.l(@mymodel.due_date, :format => :default) %>

now in the config/locales/en.yml (if english)

  date:
    formats:
      default: "%m/%d/%Y"
  time:
    formats:
      time: "%H:%M"

You may change the date format of course.

问题回答

我想date_time_attribute gem,是你的一个良好选择:

class Task < ActiveRecord::Base
  include DateTimeAttribute

  date_time_attribute :due_at
end

该编码将允许你制定<代码>到期_at_date和到期_at_time。 单独:

form_for @event do |f|
  f.text_field :due_at_date
  f.text_field :due_at_time
  f.submit
end

# this will work too:
form_for @event do |f|
  f.date_select :due_at_date
  f.time_select :due_at_time, :ignore_date => true
  f.text_field  :due_at_time_zone
  f.submit
end

# or
form_for @event do |f|
  f.date_select :due_at_date
  f.text_field  :due_at_time
  f.submit
end

例如:

task = Task.new
task.due_at                      # => nil

# Date set
task.due_at_date =  2001-02-03 
task.due_at_date                 # => 2001-02-03 00:00:00 +0700
task.due_at_time                 # => 2001-02-03 00:00:00 +0700
task.due_at                      # => 2001-02-03 00:00:00 +0700

# Time set
task.due_at_time =  10:00pm 
task.due_at_time                 # => 2013-12-02 22:00:00 +0800
task.due_at_date                 # => 2001-02-03 00:00:00 +0700
task.due_at                      # => 2001-02-03 22:00:00 +0700

# Time zone is applied as set
task.due_at_time_zone =  Moscow 
task.due_at                      # => Mon, 03 Feb 2013 22:00:00 MSK +04:00
task.due_at_time_zone =  London 
task.due_at                      # => Mon, 03 Feb 2013 22:00:00 GMT +00:00

还将允许你在时间区玩.,使用慢性等。





相关问题
rails collection_select vs. select

collection_select and select Rails helpers: Which one should I use? I can t see a difference in both ways. Both helpers take a collection and generates options tags inside a select tag. Is there a ...

SSL slowness in EC2

We ve deployed our rails app to EC2. In our setup, we have two proxies on small instances behind round-robin DNS. These run nginx load balancers for a dynamically growing and shrinking farm of web ...

Auth-code with A-Za-z0-9 to use in an URL parameter

As part of a web application I need an auth-code to pass as a URL parameter. I am currently using (in Rails) : Digest::SHA1.hexdigest((object_id + rand(255)).to_s) Which provides long strings like : ...

RubyCAS-Client question: Rails

I ve installed RubyCAS-Client version 2.1.0 as a plugin within a rails app. It s working, but I d like to remove the ?ticket= in the url. Is this possible?

activerecord has_many :through find with one sql call

I have a these 3 models: class User < ActiveRecord::Base has_many :permissions, :dependent => :destroy has_many :roles, :through => :permissions end class Permission < ActiveRecord::...

Ordering a hash to xml: Rails

I m building an xml document from a hash. The xml attributes need to be in order. How can this be accomplished? hash.to_xml

Text Editor for Ruby-on-Rails

guys which text editor is good for Rubyonrails? i m using Windows and i was using E-Texteditor but its not free n its expired now can anyone plese tell me any free texteditor? n which one is best an ...

How to get SQL queries for each user where env is production

I’m developing an application dedicated to generate statistical reports, I would like that user after saving their stat report they save sql queries too. To do that I wrote the following module: ...

热门标签