2

I have a check-box inside a form.I need if user will checked this check-box suddenly the action will execute and it will check the check-box status whether it is checked or not using Rails 3.My form is given below.

paym.html.erb:

<%= form_for :add_payment,:url => {:action => 'add_payment' },remote: true do |f| %>
<table class="table table-bordered">
        <colgroup>
            <col class="col-md-1 col-sm-1">
            <col class="col-md-1 col-sm-1">
            <col class="col-md-3 col-sm-3">
            <col class="col-md-3 col-sm-3">
            <col class="col-md-4 col-sm-4">
        </colgroup>
        <thead>
            <tr>
                <th class="text-center"><input type="checkbox"></th>
                <th class="text-center">Sl. No</th>
                <th class="text-center">Date</th>
                <th class="text-center">Receipt No.</th>
                <th class="text-center">Amount</th>
            </tr>
        </thead>
        <tbody>
            <% @result.each do |r| %>
            <tr>
                <th class="text-center"><%= check_box_tag :check_value,nil,:id => "checkbox1-1"  %></th>
                <td class="text-center"><%= r.id %></td>
                <td class="text-center"><%= r.c_date %></td>
                <td class="text-center"><%= r.Receipt_No %></td>
                <td class="text-center"><i class="fa fa-rupee"></i><%= r.v_amount %></td>
            </tr>
            <% end %>
     </tbody>
</table>
<% end %>

paym_controller.rb:

class PaymentsController < ApplicationController
def add_payment

    end
end

Here i have a table with some value .I also need when user will checked that box 1-It will check whether check box is checked or not,2-If checked then it will get all table value corresponding to that check-box.Please help me.

satya
  • 3,508
  • 11
  • 50
  • 130

1 Answers1

0
<%= check_box_tag :check_value,nil,:id => "checkbox1-1"  %>

You had pass nil value instead pass the object r like:

 <%= check_box_tag :check_value, :value => r, :id => "checkbox1-1"  %>

Now using jquery you can check if checkbox is checked then retrieve it's value. In form of value you will get particular object r and from that you can fetch your required data.

I hope this may helps you :)

Gagan Gami
  • 10,121
  • 1
  • 29
  • 55
  • @ Gagan : If I applied onchange event to submit the form That mean when user will change status of that check-box the action will execute,In that case how i will check the check-box is checked or not instead of Jquery. – satya Apr 29 '15 at 09:23
  • You can check by its `checked` property weather it is true or false? [check this question](http://stackoverflow.com/questions/13616554/how-to-make-a-check-box-checked-in-rails) – Gagan Gami Apr 29 '15 at 09:28
  • @ Gagan :How can i submit this form when check-box will checked. – satya Apr 29 '15 at 09:55
  • @satya : I think you have mixup your form and index table... Is this your form code? Where is submit button? – Gagan Gami Apr 29 '15 at 10:04
  • @ Gagan : I dont need to button to submit.But when check-box will checked the form will submit. Is this possoble? – satya Apr 29 '15 at 10:07
  • @satya : Yes it is possible but for that you have to make a call of ajax – Gagan Gami Apr 29 '15 at 10:09
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/76523/discussion-between-satya-and-gagan-gami). – satya Apr 29 '15 at 10:17