1

I am using Rails 2.3 and I don't know how to retrieve the value of a text_field of a form in order to handle it in the rjs file. I searched on the Internet all day and I didn't find a solution.

Here is my code to be more specific :

In my form.erb file :

<%= observe_field 'issue_'+@issue.id.to_s+'_estimated_hours',
  :url=>{:action => 'check_estimated_hours_field'},
  :with => "'number=' + escape(value) + 
        '&fields_estimated_ids=#{@fields_estimated_ids}'" 
%>

Controller :

def check_estimated_hours_field
    @fields_estimated_ids = params[:fields_estimated_ids]
end

check_estimated_hours_field.rjs :

for @field_id in @fields_estimated_ids
     # here I want to add all the values of fields and retrieve the result in a ruby variable 
end

How can I manage to solve the problem I encounter in the comment in check_estimated_hours_field.rjs ?

Aaron Gray
  • 11,283
  • 7
  • 55
  • 61
Jerem
  • 191
  • 1
  • 9

1 Answers1

1

After several tests and research, it is apparently impossible to recover the value of a form field with RJS. The solution I found is to create a string containing all fields values ​​in the observe_field and parse this string to the RJS to retrieve the values ​​like this:

In my form.erb file :

<% = observe_field 'issue_' + @issue.id.to_s +'_estimated_hours'
  : url => {: action => 'check_estimated_hours_field'},
  : with => "'number =' + escape (value) +
    '& fields_estimated_values ​​=' + $ ('my_form'). select ('[class ~ = estimated_hours]'). collect (function (n) {
       return n.value + '_';
    }) + "
%>

In the controller:

def check_estimated_hours_field
  estimated_hours_values ​​= params [:estimated_hours_values​​].split('_')
  @total_estimated_hours = 0
  estimated_hours_values.each {| value |
    @total_estimated_hours += hours.to_f
  }
end

In this way I can add all the values ​​and retrieve them in the variable @total_estimated_hours

Jerem
  • 191
  • 1
  • 9