我有一个包含ActiveModel的过滤器类,它包含两个日期:
class MealFilter
include ActiveModel::Model
attribute :day_from, Date
attribute :day_to, Date
end
该模型将呈现为以下形式:
<%= form_for(@filter) do |f| %>
<div class="form-group form-group--date">
<%= f.label :day_from %>
<%= f.date_select :day_from %>
</div>
<div class="form-group form-group--date">
<%= f.label :day_to %>
<%= f.date_select :day_to %>
</div>
<% end %>
现在问题是,当提交表单时,它会将此参数发送给控制器:
{"utf8"=>"✓", "meal_filter"=>{"day_from(1i)"=>"2016", "day_from(2i)"=>"1", "day_from(3i)"=>"29", "day_to(1i)"=>"2016", "day_to(2i)"=>"1", "day_to(3i)"=>"30"}, "commit"=>"Filter"}
我通过Controller参数提取值:
def meal_filter_params
if params.has_key? :meal_filter
params.require(:meal_filter).permit(:day_from, :day_to)
end
end
如果我现在使用params[:meal_filter]
将MealFilter
分配到@filter = MealFilter.new(meal_filter_params)
班级,则我的日期字段无法正确更新。它会将 1i,2i,3i 部分未正确分配到日期。
但是,如果使用ActiveRecord
类,这可以正常工作。
我是否想念一些包含?有谁知道,如果没有ActiveModel::Model
?
答案 0 :(得分:1)
在将应用程序从Rails 3.2升级到5.0时遇到了这个问题
我如何整理如下:
对于Rails 5
class MealFilter
include ActiveRecord::AttributeAssignment
attr_reader :day_from
def initialize(attributes = {})
self.attributes = attributes || {}
end
def day_from=(value)
@day_from = ActiveRecord::Type::Date.new.cast(value)
end
end
对于Rails 4.2
class MealFilter
include ActiveRecord::AttributeAssignment
attr_accessor :day_from
def initialize(attributes = {})
self.attributes = attributes || {}
end
def type_for_attribute(name)
case name
when "day_from" then ActiveRecord::Type::Date.new
end
end
end
然后您可以做:
attributes = { "day_from(3i)" => "1", "day_from(2i)" => "9", "day_from(1i)" => "2020" }
meal_filter = MealFilter.new(attributes)
meal_filter.day_from
# => Tue, 01 Sep 2020
答案 1 :(得分:0)
好的,找到了解决方案。 我需要的是MultiparameterAssignment,它实际上是在ActiveRecord中实现的,而不是在ActiveModel中实现的。
据我所见,有一个应该解决此问题的公开拉取请求(https://github.com/rails/rails/pull/8189)。
但与此同时,一些聪明的家伙写了一个可以包含在模型中的模块:https://gist.github.com/mhuggins/6c3d343fd800cf88f28e
您需要做的就是包含关注点并定义一个class_for_attribute
方法,该方法返回您的属性应该映射到的类 - 在我的案例Date
中。
答案 2 :(得分:0)
您可以简单地从date_select帮助器中访问1i,2i和3i参数,并将它们组合在一起以在before_validation回调中创建新的Date:
class DateOfBirth
include ActiveModel::Model
include ActiveModel::Attributes
include ActiveModel::Validations::Callbacks
attribute :date_of_birth, :date
attribute "date_of_birth(3i)", :string
attribute "date_of_birth(2i)", :string
attribute "date_of_birth(1i)", :string
before_validation :make_a_date
validates :date_of_birth, presence: { message: "You need to enter a valid date of birth" }
def make_a_date
year = self.send("date_of_birth(1i)").to_i
month = self.send("date_of_birth(2i)").to_i
day = self.send("date_of_birth(3i)").to_i
begin # catch invalid dates, e.g. 31 Feb
self.date_of_birth = Date.new(year, month, day)
rescue ArgumentError
return
end
end
end