N
N
Nazar Jeveling2014-08-05 23:09:53
JavaScript
Nazar Jeveling, 2014-08-05 23:09:53

How to pass multiple parameters in Rails?

I have a check_box_tag with a JS script that passes the task_status parameter:

<%= check_box_tag :status, 'complete', task.status, task_id: task.id  %>
            <% session[: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>

don't be horrified that the task_id is passed through the session, it's just for show.
Here is the controller:
def task_status

    @task = Task.find(session[:task_id])

    if @task.status == COMPLETE_STATUS
      @task.status = nil
    else
      render text: 'success' if @task.status = params[:task_status]
    end

    @task.save!
  end


So I need to pass both the task_id: task.id

UPD1

Everything worked, but only for the first task in the list, that is, when I click on some other one, nothing happens, the log is completely silent, there is a suspicion that javascript is simply not being executed

Answer the question

In order to leave comments, you need to log in

2 answer(s)
V
Viktor Vsk, 2014-08-05
@viktorvsk

It's hard not to be horrified
About sessions, forget
Get-requests the state of the server does not change, for this purpose they came up with a post and the rest
You, in a good way, should have a route like:
and in JS something like:

$('#checkbox').change(function(){
$.ajax({
url: '/task/1/state',
data: { status: $(this).val() },
method: "PATCH"
})
})

It's not worth copying, I can't vouch for performance, but the idea is as follows
Take the rails, understand REST. There will be a lot of negative emotions if you go against the system, in your own way. In rails, the main thing is convention.

R
Rinat Shaikhutdinov, 2014-08-13
@rinat_crone

You bind by ID $('#status')Accordingly, the handler is hung up only on the first checkbox on the page (you know that the element ID must be unique in the DOM, right?). Select checkboxes in some other way. I would make some data-attribute for the checkbox and bind to it

$('body').on('change', '[data-some-key]', function() {...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question