Answer the question
In order to leave comments, you need to log in
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>
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
Answer the question
In order to leave comments, you need to log in
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"
})
})
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 questionAsk a Question
731 491 924 answers to any question