0

我正在研究非常复杂和动态的表单,我根据使用 jQuery 为下拉菜单选择的值对各种方法进行大量调用以呈现部分。问题是,在填写表单后,如果验证失败,表单会在重新加载时丢失所有填写的值。我通过将一些特定值从 params{} 哈希发送到重新加载部分的方法来解决这个问题。但这非常麻烦,而且我在 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"=>"{&quot;client_asset_id&quot;=&gt;&quot;&quot;, &quot;status_id&quot;=&gt;&quot;6&quot;, &quot;server_
name&quot;=&gt;&quot;&quot;, &quot;media_id&quot;=&gt;&quot;11&quot;, &quot;serial_number&quot;=&gt;&quot;&quot;, &quot;evidence_number&quot
;=&gt;&quot;qwe&quot;, &quot;notes&quot;=&gt;&quot;&quot;, &quot;model&quot;=&gt;&quot;&quot;, &quot;manufacturer_id&quot;=&gt;&quot;69&quot
;, &quot;interface&quot;=&gt;&quot;SATA&quot;, &quot;obtained_from&quot;=&gt;&quot;wr&quot;, &quot;evidence_type_id&quot;=&gt;&quot;1&quot;}
", "media_type"=>"Server", "parent_form_action"=>"quick_save"}

如何将此原始字符串转换为控制器中的哈希?

{&quot;client_asset_id&quot;=&gt; 

需要转换为

{"client_asset_id"=>"",... etc}

===========

好的,我尝试了汤姆的方法。这会将参数生成为以下形状的字符串。我试图通过对其进行 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,我已经到了拥有以下字符串的地步。我需要将其转换回参数哈希。如何?

{"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])

它将以下带引号的字符串发送到我的控制器。

{&quot;evidence_number&quot;:&quot;&quot;,&quot;notes&quot;:&quot;&quot;,&quot;size&quot;:&quot;&quot;,&quot;model&quot;:&quot;&quot;,&quot;evidence_type_id&quot;:&quot;1&quot;,&quot;media_id&quot;:&quot;1&quot;,&quot;obtained_from&quot;:&quot;&quot;,&quot;status_id&quot;:&quot;6&quot;,&quot;manufacturer_id&quot;:&quot;&quot;,&quot;size_unit&quot;:&quot;GB&quot;}

我使用 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

有人可以告诉我如何纠正这个吗?

4

3 回答 3

2

尝试 <%=raw params[:action]%> 和 <%=raw params[:collections_controller_ev0]%>
我认为它会正常工作。

$.post("/collections/show_selected_media_fields",{media_type: $("#collections_controller_ev0_media_id option:selected").text(), parent_form_action: <%=raw params[:action]%>,ev0_manufacturer_id:<%=raw params [:collections_controller_ev0].inspect%>, }, function(data) {$("#show_selected_media_fields").html(data);});

于 2012-12-01T05:07:22.360 回答
0

params基本上是一个哈希。所以你可以使用.to_json它。然后在您的控制器中,您可以将其从 JSON 转换为带有JSON.parse().

如果您打算在 Javascript 或 HTML 中处理它,通常使用 JSON 是一个更好的主意。

于 2012-11-30T21:12:27.567 回答
-1

我上次编辑作品中显示的方法!它产生正确的哈希。

于 2012-12-03T19:49:28.910 回答