Forum: Ruby on Rails Default action performed by jQuery on the partial data in ruby on rails

Posted by sharath chander punthambekar (shrath001)
on 2012-12-18 09:17
Problem :- Default action performed by jQuery on the partial data in
ruby on rails


description :- I am attempting the following. A customer select option
in the main form. when a user selects a customer in the main form his
bills should appear in a select format in all the subsequent partials.
Nested form gem is being used to get the partial.

Problem :- as i select the customer i am able to get the customer data,
here the customer bills, but i am not able to post the data in the
partial. the div id where i am supposed to post the data is not been
detected. Bills concerning only the customer selected, does not appear
in the partial.

solutions Tried. 1. rewrote the entire code thinking problem might be
with the data but failed.

2. changed the environment to windows thinking problem with the
environment  but the same problem is appearing in windows  too.

3. created a test project with one field in main form and one field in
the partial to see if the problem is there in the main project but same
error appears in the test project also.

4. Tried to send customer data to the partial and yet still the same
error appears.

5. shifted the entire employee, customer and supplier code to partial.
this solves the problem of getting bills, but allows user to select
employee or supplier in the subsequent partials, fails validation,
because each partial is a separate entity and changes done in first
partial is not reflected in subsequent partial.

6. tried to achieve the above problem without using jquery with rails
code but java-script is very much required.

7. changed the nested form gem to cocoon gem thinking the problem is
with the gem, but the same problem occurs.
Posted by Colin Law (Guest)
on 2012-12-18 18:20
(Received via mailing list)
On 18 December 2012 08:17, sharath chander punthambekar
<lists@ruby-forum.com> wrote:
> here the customer bills, but i am not able to post the data in the
> partial. the div id where i am supposed to post the data is not been
> detected. Bills concerning only the customer selected, does not appear
> in the partial.

Your description is so vague that it is impossible to provide useful
help.  First have a look at the Rails Guide on Debugging, this will
show you techniques to help debug your code.  Using those techniques
work out exactly where the problem is arising.  Once you know where
the problem is arising then if you still cannot see the answer post
again here showing the section of the code that is not working as you
expect.

Colin
Posted by sharath chander punthambekar (shrath001)
on 2012-12-18 18:32
Colin Law wrote in post #1089479:
> On 18 December 2012 08:17, sharath chander punthambekar
> <lists@ruby-forum.com> wrote:
>> here the customer bills, but i am not able to post the data in the
>> partial. the div id where i am supposed to post the data is not been
>> detected. Bills concerning only the customer selected, does not appear
>> in the partial.
>
> Your description is so vague that it is impossible to provide useful
> help.  First have a look at the Rails Guide on Debugging, this will
> show you techniques to help debug your code.  Using those techniques
> work out exactly where the problem is arising.  Once you know where
> the problem is arising then if you still cannot see the answer post
> again here showing the section of the code that is not working as you
> expect.
>
> Colin

Thank you Colin. I will revert back with the code strip shortly. Sorry 
to have been vague.
Sharath
Posted by sharath chander punthambekar (shrath001)
on 2012-12-19 06:51
sharath chander punthambekar wrote in post #1089481:
> Colin Law wrote in post #1089479:
>> On 18 December 2012 08:17, sharath chander punthambekar
>> <lists@ruby-forum.com> wrote:
>>> here the customer bills, but i am not able to post the data in the
>>> partial. the div id where i am supposed to post the data is not been
>>> detected. Bills concerning only the customer selected, does not appear
>>> in the partial.
>>
>> Your description is so vague that it is impossible to provide useful
>> help.  First have a look at the Rails Guide on Debugging, this will
>> show you techniques to help debug your code.  Using those techniques
>> work out exactly where the problem is arising.  Once you know where
>> the problem is arising then if you still cannot see the answer post
>> again here showing the section of the code that is not working as you
>> expect.
>>
>> Colin
>
> Thank you Colin. I will revert back with the code strip shortly. Sorry
> to have been vague.
> Sharath

I am attempting the following. A customer select option in the main
form. when a user selects a customer in the main form his bills should
appear in the all the subsequent partials.

I am using ryan bates's nested_form gem to get the partial. the code is
as follows.
<%= simple_nested_form_for @money_receipt do |f| %>

    <div class="customers"><%= f.collection_select :customer_id,
Customer.all, :id, :name, options ={:prompt => "-Select a Customer"},
:style=>'width:210px;'%></div><br />
    /*rest of the code*/

    <%= f.link_to_add "Add line items", :money_receipt_line_items %>

inside the partial
<div class="bill">
<%= f.collection_select :customer_bill_id, CustomerBill.all,:id,:id,
{:prompt => "Select Customer bill"}, :style => 'width:202px;'
%></div><br />

/*rest of the code*/

the jQuery code for the above is as follows
jQuery(document).ready(function(){
   jQuery(".customers select").bind("change", function() {
      var data = {
        customer_id: jQuery(this).val()
      }
      jQuery.getJSON(
         "/money_receipts/get_bills",
        data, function(data){
      var result = "",a,x;
      var b = "<option>Select customer Bill</option>"
      for(i=0;i<data.length; i++)
      {
       a = data[i];
       result = result + "<option value="+a[0]+">"+a[0]+"</option>";
       console.log(result);
      }
     child=jQuery('.bill select').html(b+result);
    });
    });
  });

and finally in the controller i have. def get_bills
@bill_amount =
CustomerBill.find_all_by_customer_id(params[:customer_id]).map{|bill|
[bill.id]} if params[:customer_id]
    respond_to do |format|
      format.json { render json: @bill_amount }
    end
  end

i was attempting to filter the bills and put in the div id bill. Now if
i do a console.log in jQuery code i am getting the customer_id and the
bills but i am not able to post it. if i do console.log on the child i
get output as this jQuery(). where am i going wrong? is there a better
way to achieve the above? guidance required. Thanks in advance.

The fault area I feel is in jquery. A little more explanation is below;
jQuery
1 jQuery(document).ready(function(){
2   jQuery(".customers select").bind("change", function() {
3      var data = {
4        customer_id: jQuery(this).val()
5      }
6      jQuery.getJSON(
7         "/money_receipts/get_bills",
8        data, function(data){
9      var result = "",a,x;
10      var b = "<option>Select customer Bill</option>"
11      for(i=0;i<data.length; i++)
12      {
13       a = data[i];
14       result = result + "<option value="+a[0]+">"+a[0]+"</option>";
15       console.log(result);
16      }
17     child=jQuery('.bill select').html(b+result);

18     console.log(child);
19    });
20    });
21  });

Line number 14 i am able to get the result, i.e i am able to get the 
bills. at line number 17 i am not able to post it. the next line i.e 
line 18 i did a console.log of line 17 i am getting "jQuery ()" as 
output but i must get the list of bills as output.

Div id "bill" in the partial is not been detected.
Please log in before posting. Registration is free and takes only a minute.
Existing account (Switch to SSL-encrypted connection)
NEW: Do you have a Google/GoogleMail or Yahoo account? No registration required!
Log in with Google account | Log in with Yahoo account
No account? Register here.