我正在处理非常复杂和动态的表单,我会根据使用jQuery为dropdown选择的值,对各种方法进行大量调用以呈现部分。问题是填写表格后如果验证失败,表格会在重新加载时丢失所有填写的值。我通过将params {} hash中的一些特定值发送到重新加载的部分方法来解决这个问题。但它非常麻烦,我在params hash中有大量元素。如何使用jQuery将整个params {}发送到同一个控制器中的另一个方法?
好的,我以我的形式尝试了这个:
$.post("/collections/show_selected_media_fields",{media_type: $("#collections_controller_ev0_media_id option:selected").text(), parent_form_action: "<%=params[:action]%>",ev0_manufacturer_id:"<%=params[:collections_controller_ev0].inspect%>", }, function(data) {$("#show_selected_media_fields").html(data);});
它产生以下字符串作为参数发送:
Parameters: {"ev0_manufacturer_id"=>"{"client_asset_id"=>"", "status_id"=>"6", "server_
name"=>"", "media_id"=>"11", "serial_number"=>"", "evidence_number"
;=>"qwe", "notes"=>"", "model"=>"", "manufacturer_id"=>"69"
;, "interface"=>"SATA", "obtained_from"=>"wr", "evidence_type_id"=>"1"}
", "media_type"=>"Server", "parent_form_action"=>"quick_save"}
如何将此原始字符串转换为控制器中的哈希?
{"client_asset_id"=>
需要转换为
{"client_asset_id"=>"",... etc}
===========
好的,我试过汤姆的方法。这会将params生成为以下形状的字符串。我尝试通过对其进行eval将其转换为哈希值。但它出错了。
{commit=>Save, ev1_current_location_id=>, collections_controller_ev1=>{file_system=>NTFS, obtained_from=>, evidence_number=>, interface=>SAT
A, size_unit=>GB, manufacturer_id=>, encryption_version=>, media_id=>3, size=>, evidence_type_id=>3, other_encryption=>, encryption_method=>
N/A, serial_number=>, model=>, encryption_key=>}, ev0_from_location_category=>, ev0_obtained_from_email_id=>, collections_controller_ev0=>{o
btained_from=>, evidence_number=>, media_id=>1, evidence_type_id=>1, status_id=>6}, custody_action=>Create, collection=>{acquired_by=>Amande
ep Singh, custodian_id=>12, matter_id=>58, location=>sa Nose, client_id=>11, software_version=>, collection_date_time=>Fri Nov 30 14:28:06 -
0800 2012, acquisition_method=>Direct Collection, notes=>, software_id=>1}, _method=>put, utf8=>Γ£ô, ev1_current_location_category=>, ev0_cu
rrent_location_category=>, add_working_copy=>No, ev1_obtained_from_email_id=>, authenticity_token=>3vyn6057DDIyfgTnbckeh5heRTIgcVBfxtY89Krfr
/c=, ev1_existing_artifact_type=>, ev0_from_location_id=>, action=>quick_save, ev1_from_location_id=>, ev1_from_location_category=>, ev0_cur
rent_location_id=>, controller=>collections}
使用.to_json和HTMLEntities gem我得到了一个点,我有以下字符串。我需要将它转换回params hash。怎么样?
{"utf8"=>"Γ£ô","collection"=>{"acquired_by"=>"Amandeep Singh","notes"=>"","matter_id"=>"58","software_id"=>"1","acquisition_method"=>"Direct
Collection","client_id"=>"11","custodian_id"=>"0","collection_date_time"=>"Fri Nov 30 15=>52=>12 -0800 2012","software_version"=>"","locati
on"=>"sa Nose"},"ev0_current_location_id"=>"","ev1_existing_artifact_type"=>"","ev1_obtained_from_email_id"=>"","custody_action"=>"Create","
action"=>"quick_save","ev0_current_location_category"=>"","ev1_from_location_category"=>"","_method"=>"put","ev0_obtained_from_email_id"=>""
,"ev0_from_location_category"=>"","ev1_current_location_category"=>"","commit"=>"Save","controller"=>"collections","authenticity_token"=>"3v
yn6057DDIyfgTnbckeh5heRTIgcVBfxtY89Krfr/c=","ev0_from_location_id"=>"","ev1_current_location_id"=>"","collections_controller_ev0"=>{"status_
id"=>"6","media_id"=>"9","obtained_from"=>"","evidence_number"=>"","evidence_type_id"=>"1"},"collections_controller_ev1"=>{"media_id"=>"3","
encryption_key"=>"","encryption_version"=>"","size"=>"","obtained_from"=>"","encryption_method"=>"N/A","model"=>"","evidence_number"=>"","ev
idence_type_id"=>"3","size_unit"=>"GB","other_encryption"=>"","interface"=>"SATA","file_system"=>"NTFS","serial_number"=>"","manufacturer_id
"=>""},"ev1_from_location_id"=>"","add_working_copy"=>"No"}
==========编辑==========
我在视图中使用以下内容。
JSON( params[:collections_controller_ev0])
它将以下引用的字符串发送给我的控制器。
{"evidence_number":"","notes":"","size":"","model":"","evidence_type_id":"1","media_id":"1","obtained_from":"","status_id":"6","manufacturer_id":"","size_unit":"GB"}
我使用gsub('“','”')将其转换为有效的JSON字符串,如下所示。输出是VALID JSON字符串,由http://jsonlint.com/验证如下所示。
{"evidence_number":"","notes":"123","size":"123","model":"123","evidence_type_id":"1","media_id":"1","obtained_from":"","status_id":"6","manufacturer_id":"69","size_unit":"GB"}
JSON.parse在此字符串上无错误地工作,但不会生成正确形成的哈希。它产生以下:
notes123evidence_numbersize123model123evidence_type_id1media_id1obtained_fromstatus_id6size_unitGBmanufacturer_id69
有人可以告诉我如何纠正这个问题吗?
答案 0 :(得分:2)
尝试&lt;%=原始参数[:action]%&gt;和&lt;%= raw params [:collections_controller_ev0]%&gt;而是
我认为它会正常工作。
$ .post(“/ collections / show_selected_media_fields”,{media_type:$(“#collections_controller_ev0_media_id option:selected”)。text(),parent_form_action:&lt;%= raw params [:action]%&gt;,ev0_manufacturer_id:&lt;% = raw params [:collections_controller_ev0] .inspect%&gt;,},function(data){$(“#show_selected_media_fields”)。html(data);});
答案 1 :(得分:0)
params
基本上是一个哈希。所以你可以使用.to_json
。然后在您的控制器中,您可以将其从JSON转换为具有JSON.parse()
的哈希。
如果您要使用Javascript或HTML处理它,通常使用JSON会更好。
答案 2 :(得分:-1)
我上次编辑工作中显示的方法!它产生正确的哈希。