Rails: Mapping a Date from form parameters into ActiveModel object -


i have filter-class includes activemodel , consists of 2 dates:

class mealfilter   include activemodel::model    attribute :day_from, date   attribute :day_to, date end 

that model rendered form following:

  <%= form_for(@filter) |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 %> 

the problem now, when form gets submitted, sends parameters controller:

{"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"} 

i extract values via controller parameters:

def meal_filter_params   if params.has_key? :meal_filter     params.require(:meal_filter).permit(:day_from, :day_to)   end end 

if assign params[:meal_filter] mealfilter class @filter = mealfilter.new(meal_filter_params), date fields not updated correctly. seams 1i, 2i, 3i parts not correctly assigned dates.

however, works fine if used activerecord class.

do miss include? know, magic mapping implemented if not in activemodel::model?

ok, found solution. needed multiparameterassignment implemented in activerecord not in activemodel.

as far can see, there open pull request (https://github.com/rails/rails/pull/8189) should resolve issue.

but in meantime, clever guy wrote module can included model: https://gist.github.com/mhuggins/6c3d343fd800cf88f28e

all need include concern , define class_for_attribute method returns class attribute should mapped - in case date.


Comments