A PHP Error was encountered
Severity: Notice
Message: Undefined index: userid
Filename: views/question.php
Line Number: 191
Backtrace:
File: /var/www/html/cnasolution/application/views/question.php
Line: 191
Function: _error_handler
File: /var/www/html/cnasolution/application/controllers/Questions.php
Line: 419
Function: view
File: /var/www/html/cnasolution/index.php
Line: 315
Function: require_once
After javascript submits form in rails 6 the update action in controller not launching update.js.erb
Not sure if I am asking this question wrong, but I can't seem to find exactly the issue I am faced with.
I have a very basic rails 6 app for a task list. Tasks are either complete or incomplete and the change/update of this is to be done via javascript (the html side works just fine).
Here is my form partial _task.html.erb:
<%= form_with model: task, html: { class: "edit_task", id: "edit_task_#{task.id}" } do |f| %> <%= f.check_box :complete %> <%= f.submit "Update" %> <%= f.label :complete, task.name %> <%= link_to "(remove)", task, method: :delete, data: {confirm: "Are you sure?"}, remote: true %> <% end %>
Here is the javascript to submit the form, tasks.js
function submit_update(){ const checkboxes = document.querySelectorAll('.edit_task input[type=checkbox]'); const submitbutton = document.querySelectorAll('.edit_task input[type=submit]'); submitbutton.forEach(button => button.remove()); checkboxes.forEach(checkbox => { checkbox.addEventListener('click', () => checkbox.parentElement.submit()); }); } window.addEventListener('turbolinks:load', event => { submit_update(); document.addEventListener('task_added', event => submit_update()); });
This part works just fine, but once submitted and based on this section of the controller
def update @task.update!(task_params) respond_to do |format| format.html { redirect_to root_url, notice: 'Task successfully updated' } format.js end end
My understanding is together this should launch update.js.erb, which currently looks like
unction update_task() { const current_task = document.querySelector('#edit_task_<%= @task.id %>'); const complete_tasks = document.querySelector('#complete_tasks'); const incomplete_tasks = document.querySelector('#incomplete_tasks'); <% if @task.complete? %> complete_tasks.insertAdjacentHTML('beforeend',current_task.innerHTML); <% else %> incomplete_tasks.insertAdjacentHTML('beforeend',current_task.innerHTML); <% end %> } update_task();
I have tried changing the above to a single line using an alert call and it still never gets called.
If someone could let me know why update.js.erb is not being called, it would be much appreciated :)
If any additional information is required, please let me know?