0

私は非常に複雑で動的なフォームに取り組んでおり、jQuery を使用してドロップダウン用に選択された値に応じてパーシャルをレンダリングするために、さまざまなメソッドを多数呼び出しています。問題は、フォームに入力した後、検証に失敗した場合、再読み込み時にフォームに入力されたすべての値が失われることです。これを回避するには、再ロード時に params{} ハッシュから特定の値をパーシャルのメソッドに送信しました。しかし、それは非常に面倒で、params ハッシュには多数の要素があります。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}

===========

わかりましたトムの方法を試しました。これにより、params が次の形状の文字列として生成されます。評価を行ってハッシュに変換しようとしました。しかし、それはエラーになります。

{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 ハッシュに戻す必要があります。方法?

{"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 文字列に変換します。出力は、以下に示すhttp://jsonlint.com/によって検証された有効な JSON 文字列です。

{"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%>, }, 関数(データ) {$("#show_selected_media_fields").html(データ);});

于 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 に答える