N
N
Nazar Jeveling2014-08-04 14:08:48
JavaScript
Nazar Jeveling, 2014-08-04 14:08:48

How to set check_box_tag in Rails using JavaScript?

I have a task, I need to put a check_box_tag, by clicking on which the status of the task should change from false to true and vice versa.
Here is what I tried and it didn't work

<%= check_box_tag 'status', 'complete', task.status, data: { action: 'task_status', remote: true, task_id: task.id } %>

here is what i added to the controller
def task_status
    @task = Task.find(params[:task_id])
    if @task.status == COMPLETE_STATUS 
      @task.status = nil
    else
      @task.status = COMPLETE_STATUS
    end
    @task.save
  end

UPD1
After seeing a similar case on stackoverflow, here is what I came up with
<%= check_box_tag :status, 'complete', task.status %>

<script>
$('#status').change(function() {
  $.get('todo/task_status?task_id='+$(task.id).val(), function(data, status) {
    if(status == 'success'){
      alert(data)
    }
  })
});
</script>

and here is the method in the controller
def task_status
    @task = Task.find(params[:task_id])

    if @task.status == COMPLETE_STATUS
      @task.status = nil
    else
      @task.status = COMPLETE_STATUS
    end

    render text: 'success'

UPD2 The above is nonsense, since I need to pass the id of the task on which the checkbox is enabled
, that's what I got to
<%= check_box_tag :status, 'complete', task.status, task_id: task.id  %>

            <script>
              $('#status').change(function() {
                $.get('todo/task_status?task_status='+$(this).val(), function(data, status) {
                  if(status == 'success'){
                    alert(data)
                  }
                })
              });
            </script>

and here is what is in the controller
def task_status
    
    @task = Task.find(params[:task_id])

    @task.status = params[:task_status]

    render text: 'success'

  end

I set up routing, now the problem is that the task_id parameter is not passed: task.id, how can it be passed to the controller?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
I
Ilya Lozer, 2014-08-05
@ammet

stackoverflow.com/questions/14780929/rails-checkbo...
Same problem solved

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question