2012-03-20 80 views
2

我即將撕我的頭髮,所以我一定是失去了一些東西基本...Rails的date_select和update_attributes方法

我有以下形式(簡體):

<%= form_for(@public_call) do |f| %> 
<div>Start: <%= f.date_select(:start_date,:start_year=>Time.now.year) -%></div> 
<div>End: <%= f.date_select(:end_date,:start_year=>Time.now.year) -%></div> 
<div>Deadline: <%= f.datetime_select(:submission_deadline,:start_year=>Time.now.year-1) -%></div> 
<%- end -%> 

形式帖子通過以下的方法(簡化):

def update 
    begin 
    @public_call = PublicCall.find(params[:id]) 
    @public_call.update_attributes!(params[:public_call]) 
    rescue 
    ... 
    end 
end 

1.5.2由軌道記錄器所報告的下面的形式的結果(簡化):

Parameters: 
    {"public_call"=> 
    {"start_date(1i)"=>"2012", 
    "start_date(2i)"=>"9", 
    "start_date(3i)"=>"30", 
    "end_date(1i)"=>"2012", 
    "end_date(2i)"=>"10", 
    "end_date(3i)"=>"3", 
    "submission_deadline(1i)"=>"2012", 
    "submission_deadline(2i)"=>"4", 
    "submission_deadline(3i)"=>"27", 
    "submission_deadline(4i)"=>"23", 
    "submission_deadline(5i)"=>"59"} 
    } 

下面是問題:Rails似乎沒有將三個字段的參數轉換爲數據庫的日期和日期時間對象,就像它應該的那樣。由Rails記錄器報告的查詢是:

UPDATE `public_calls` SET `start_date`=NULL, `end_date`=NULL, `submission_deadline`=NULL, `updated_at`='2012-03-20 20:47:15' WHERE... 

任何想法是什麼給出的?

BTW,Rails 3.2.0/Ruby 1.9.3。我的任何屬性在public_calls模型中都沒有被保護。

謝謝!

編輯

使用也試過:與update_attributes方法without_protection選擇!但那也行不通。

編輯#2

我試着調試發生了什麼事在的ActiveRecord的屬性分配模塊如下:

def assign_multiparameter_attributes(pairs) 
    Rails.logger.info('ASSIGN_MULTIPARAMETER_ATTRIBUTES: '+pairs.inspect) 
    execute_callstack_for_multiparameter_attributes(
    extract_callstack_for_multiparameter_attributes(pairs) 
) 
end 

def execute_callstack_for_multiparameter_attributes(callstack) 
    errors = [] 
    Rails.logger.info('EXECUTE_CALLSTACK_FOR_MULTIPARAMETER_ATTRIBUTES: '+callstack.inspect) 
    callstack.each do |name, values_with_empty_parameters| 
    begin 
     send(name + "=", read_value_from_parameter(name, values_with_empty_parameters)) 
    rescue => ex 
     errors << AttributeAssignmentError.new("error on assignment #{values_with_empty_parameters.values.inspect} to #{name}", ex, name) 
    end 
    end 
    unless errors.empty? 
    raise MultiparameterAssignmentErrors.new(errors), "#{errors.size} error(s) on assignment of multiparameter attributes" 
    end 
end 

def extract_callstack_for_multiparameter_attributes(pairs) 
    Rails.logger.info('EXTRACT_CALLSTACK_FOR_MULTIPARAMETER_ATTRIBUTES: '+pairs.inspect) 
    attributes = { } 
    pairs.each do |pair| 
    multiparameter_name, value = pair 
    attribute_name = multiparameter_name.split("(").first 
    attributes[attribute_name] = {} unless attributes.include?(attribute_name) 
    parameter_value = value.empty? ? nil : type_cast_attribute_value(multiparameter_name, value) 
    attributes[attribute_name][find_parameter_position(multiparameter_name)] ||= parameter_value 
    end 
    Rails.logger.info('EXTRACT_CALLSTACK_FOR_MULTIPARAMETER_ATTRIBUTES: '+attributes.inspect) 
    attributes 
end 

和這裏的我得到什麼:

ASSIGN_MULTIPARAMETER_ATTRIBUTES: [["start_date(1i)", "2012"], ["start_date(2i)", "9"], ["start_date(3i)", "30"], ["end_date(1i)", "2012"], ["end_date(2i)", "10"], ["end_date(3i)", "3"], ["submission_deadline(1i)", "2012"], ["submission_deadline(2i)", "4"], ["submission_deadline(3i)", "27"], ["submission_deadline(4i)", "23"], ["submission_deadline(5i)", "59"]] 
EXTRACT_CALLSTACK_FOR_MULTIPARAMETER_ATTRIBUTES: [["start_date(1i)", "2012"], ["start_date(2i)", "9"], ["start_date(3i)", "30"], ["end_date(1i)", "2012"], ["end_date(2i)", "10"], ["end_date(3i)", "3"], ["submission_deadline(1i)", "2012"], ["submission_deadline(2i)", "4"], ["submission_deadline(3i)", "27"], ["submission_deadline(4i)", "23"], ["submission_deadline(5i)", "59"]] 
EXTRACT_CALLSTACK_FOR_MULTIPARAMETER_ATTRIBUTES: {"start_date"=>{1=>2012, 2=>9, 3=>30}, "end_date"=>{1=>2012, 2=>10, 3=>3}, "submission_deadline"=>{1=>2012, 2=>4, 3=>27, 4=>23, 5=>59}} 

任何想法,爲什麼execute_callstack_for_multiparameter_attributes方法沒有被調用?

+0

我也有這個問題。你能弄明白嗎? – 2012-05-24 15:25:16

回答

0

升級你的rails版本 - 我遇到同樣的問題,從v 3.2.0移到v 3.2.8爲我解決了它。

+0

有關這些方法的rails更改日誌中有任何內容嗎? – 2012-10-04 20:51:19