1
votes

I have a form_for where I have some field related to form object and there are some fields which are not related to form object so for those field I am using text_field_tag. But those field are like key - value pair, so when submitting a form, I want to combine or rather send in hash format so that in controller code I can easily save these values together.

Here is my form_for code

[Demo]

<%= form_for [@var], :url => {:action => form_action}, :html => {:id => form_id} do |f|   %>

<table border=0 cellpadding="0" cellspacing="8" id='psp-config-form' >
<tr class='psp_config_row'>
  <td><label for=''>Screen Label</label></td>
  <td>
    <%= text_field_tag "screen_label[]", "", :size => 20, :class => 'search-txt-input', :maxlength => 360 %>
  </td>
  <td><label for=''>Value</label></td>
  <td>
    <%= text_field_tag "screen_value[]", "", :size => 20, :class => 'search-txt-input', :maxlength => 360 %>
  </td>
  <td><label for=''>Mask Value</label></td>
  <td>
    <%= check_box_tag "is_masked[]", "", false, :class => 'search-txt-input' %>
  </td>
  <td>      
    <%= link_to 'Delete', "javascript:void(0);", :class => 'linkclass delete_link', :title => 'Confirmation', :style=>"display: none" %>
  </td>
</tr>
<tr>
  <td></td>
  <td class="add_psp_config_button">
    <%= link_to "Add Additional PSP credentials", "javascript:void(0);", :title => "Add PSP Config Values", :class => "actionBtn button", :id => "add_psp_config_button" %>
  <td>
</tr>
<tr>
  <td></td>
  <td>
      <%= f.submit 'Add PSP',:class => 'loginBtn', :id => 'add-psp-button'%>          
  </td>
  <td>
    <%= text_field_tag 'cancel', 'Cancel', :type=> 'button', :class => 'loginBtn' %>        
  </td>
</tr> 

Here is the current params sent to controller:

Parameters: {"utf8"=>"?", "authenticity_token"=>"sMZYtNbYGXf3iDtYrOVafHxwzFTM4U8+V5xOyFgr5/8=", "psp_admin_portal"=>{"psp_name"=>"google", "psp_url"=>"https://google.com", "psp_username"=>"google-username", "psp_password=>"XXXX"}, "screen_label"=>["label1", "label2", "label3"], "screen_value"=>["val1", "val2", "val3"], "commit"=>"Add PSP"}

So here I am finding a way using which I can send screen_lable and value params like this:

{screen_params => {"label1" => "val1", "label2" => "val2"}, ...}

let me know if any of you have any idea on this.

Thanks, Dean

1
I have added text fileds like this <%= text_field_tag "#{f.object_name}[config_params][screen_label][1]", "", :size => 20, :class => 'screen_label search-txt-input', :maxlength => 360 %> And getting params in the following way: psp_admin_portal"=>{"psp_name"=>"kkkk", "psp_url"=>"", "psp_username"=>"", "psp_password=>"XXXX", "config_params"=>{"screen_label"=>{"1"=>"label1", "2"=>"label2", "3"=>"label3", "4"=>"label4"}, "screen_value"=>{"1"=>"val1", "2"=>"val2", "3"=>"val3", "4"=>"val4"}, "is_masked"=>{"1"=>"false", "2"=>"false", "3"=>"false"}}}, "commit"=>"Add PSP"} - Dinesh M
So in the model I am seperating the required hash in the format mentioned in ticket description - Dinesh M
is it working?.. as expected? was my answer useful? - beck03076

1 Answers

2
votes

Try using the object_name method on the form object like below,

<%= text_field_tag "#{f.object_name}[your_variable_name]", "", :size => 20, :class => 'search-txt-input', :maxlength => 360 %>

If you submit it now, the form object's hash will have this element, "your_variable_name" => "value_entered_in_text_box".