What Does ActiveRecord::MultiparameterAssignmentErrors Mean?

后端 未结 7 654
[愿得一人]
[愿得一人] 2020-12-15 22:46

I have a rails form with a datetime_select field. When I try to submit the form, I get the following exception:

ActiveRecord::MultiparameterAssignmentErrors          


        
7条回答
  •  刺人心
    刺人心 (楼主)
    2020-12-15 23:19

    Super hack, but I needed to solve this problem right away for a client project. It's still a bug with Rails 2.3.5.

    Using either date_select or datetime_select, if you add this to your model in the initialize method, you can pre-parse the passed form-serialized attributes to make it work:

    def initialize(attributes={})
      date_hack(attributes, "deliver_date")
      super(attributes)
    end
    
    def date_hack(attributes, property)
      keys, values = [], []
      attributes.each_key {|k| keys << k if k =~ /#{property}/ }.sort
      keys.each { |k| values << attributes[k]; attributes.delete(k); }
      attributes[property] = values.join("-")
    end
    

    I am using this with a nested, polymorphic, model. Here's a question I had showing the models I'm using. So I needed accepts_nested_attributes_for with a datetime.

    Here's the input and output using the console:

    e = Event.last
    => #
    e.model_surveys
    => []
    e.model_surveys_attributes = [{"survey_id"=>"864743981", "deliver_date(1i)"=>"2010", "deliver_date(2i)"=>"2", "deliver_date(3i)"=>"11"}]
    PRE ATTRIBUTES: {"survey_id"=>"864743981", "deliver_date(1i)"=>"2010", "deliver_date(2i)"=>"2", "deliver_date(3i)"=>"11"}
    # run date_hack
    POST ATTRIBUTES: {"survey_id"=>"864743981", "deliver_date"=>"2010-2-11"}
    e.model_surveys
    => [#]
    >> e.model_surveys.last.deliver_date.class
    => ActiveSupport::TimeWithZone
    

    Otherwise it was either null, or it would throw the error:

    1 error(s) on assignment of multiparameter attributes

    Hope that helps, Lance

提交回复
热议问题