Alessandra Cardoso Alessandra Cardoso - 3 months ago 14
Javascript Question

Rails 4 get value from text_field_tag that contains javascript var

I'm migrating from rails 2.3.10 to rails 4, and I have a app for send message on cellphone to patient schedule. My app show a list of schedule's patient and for each line exist a check_box_tag, when I select one or more and send message.
So I have a form that call a partial and this partial contains a list with check_box_tag. Each check box selected is storage on javascript variable selected_ids and return on text_field_tag in the main form. The problem is on event link_to the parameter selected_ids is empty, but I can see on the check_box_tag
Below part of form, partial and javascript:



function MultiSelectIDs(FieldName) {
var selected_ids = new Array()
var objCheckBoxes = document.getElementsByName(FieldName);
for(var i = 0; i < objCheckBoxes.length; i++){
if (objCheckBoxes[i].checked) {
var x = selected_ids.splice(selected_ids.length,0, objCheckBoxes[i].id);
}
}
document.getElementById('selected_ids').value = selected_ids.join(", ")
};

<% if !@tagendamento.blank? && !@tagendamento.nil? %>
<% for tagendamento in @tagendamento do %>
<tr>
<%= check_box_tag "#{tagendamento.id}", 0, false, :name=> "chk_agendamento", :onclick => "MultiSelectIDs('chk_agendamento')" %>
<% end %>

<%= text_field_tag :selected_ids %>
<%= link_to 'Pré-Visualizar SMS',send_sms_queries_path(:sel_ids=>params[:selected_ids],:perfil=>params[:perfil], :visualizar => '1', :dt_envio=>(params[:dt_envio].nil?)? (Date.today - 3) : params[:dt_envio],:t_envio=>params[:t_envio],:tipo_msg=>params[:tipo_msg],:mensagem=>(!params[:mensagem].nil? and !params[:mensagem].blank?)? params[:mensagem] : @standard,:selec_tipotemplate_sms=>params[:selec_tipotemplate_sms],:date_agend_hc=>params[:date_agend_hc]), :target=>'_blank', :style => "color:black"%>





<%= text_field_tag :selected_ids %>
<%= link_to 'Pré-Visualizar SMS', send_sms_queries_path(:sel_ids => params[:selected_ids],
:perfil => params[:perfil],
:visualizar => '1',
:dt_envio => (params[:dt_envio].nil?)? (Date.today - 3) : params[:dt_envio],
:t_envio => params[:t_envio],
:tipo_msg => params[:tipo_msg],
:mensagem => (!params[:mensagem].nil? and !params[:mensagem].blank?)? params[:mensagem] : @standard,
:selec_tipotemplate_sms => params[:selec_tipotemplate_sms],
:date_agend_hc => params[:date_agend_hc]),
:target => '_blank',
:style => "color:black"%>

Answer

Finally worked! Added content of text_field_tag in onclick link_to as follows:

<%= link_to 'Pré-Visualizar SMS', send_sms_queries_path(:selected_ids	=>params[:selected_ids]),  
:target	=>	'_blank', 
:style	=>	"color:black",
:onclick => "this.href = this.href + '&selected_ids=' + selected_ids.value"%>

Thank you folks!