0

I am trying to AJAX-ify a to do type app and just finished dealing with this problem for my create method (see [here][1]). Now the delete method is acting up, but I can't use the previous solution (moving the remote: true from the submit to the form_for line) because the deletion is done via link_to.

Here's my tasks#index action:

  def index
    @create_task = Task.new
    @tasks = Task.all
    @one_time = Task.where(frequency: "OneTime", completed: false, user_id: current_user.id)
    @one_time_done = Task.where(frequency: "OneTime", completed: true, user_id: current_user.id)
    @daily = Task.where(frequency: "Daily", completed: false, user_id: current_user.id)
    @daily_done = Task.where(frequency: "Daily", completed: true, user_id: current_user.id)
    @weekly = Task.where(frequency: "Weekly", completed: false, user_id: current_user.id)
    @weekly_done = Task.where(frequency: "Weekly", completed: true, user_id: current_user.id)
    @monthly = Task.where(frequency: "Monthly", completed: false, user_id: current_user.id)
    @monthly_done = Task.where(frequency: "Monthly", completed: true, user_id: current_user.id)
  end

And my tasks#destroy action:

  def destroy
    @one_time = Task.where(frequency: "OneTime", completed: false, user_id: current_user.id)
    @one_time_done = Task.where(frequency: "OneTime", completed: true, user_id: current_user.id)
    @daily = Task.where(frequency: "Daily", completed: false, user_id: current_user.id)
    @daily_done = Task.where(frequency: "Daily", completed: true, user_id: current_user.id)
    @weekly = Task.where(frequency: "Weekly", completed: false, user_id: current_user.id)
    @weekly_done = Task.where(frequency: "Weekly", completed: true, user_id: current_user.id)
    @monthly = Task.where(frequency: "Monthly", completed: false, user_id: current_user.id)
    @monthly_done = Task.where(frequency: "Monthly", completed: true, user_id: current_user.id)

    if @task.destroy
        respond_to do |format|
          format.js
          format.html
      end
    else
      flash[:warning] = "Oops! Something went wrong!"
    end

  end
  [1]: https://stackoverflow.com/questions/45667684/ajax-with-rails-missing-template

Here is my delete link_to (from a partial):

   <%= link_to task_path(task), method: :delete, remote: true do %>
        <i class="icon ion-close-circled" id=(task.id + "task") style="margin-left: 5px"></i>
   <% end %>

Rendered on tasks/index.html.erb like so:

<div id="onetime-todo"><%= render partial: 'items', locals: { task: @one_time } %></div>

Here's my delete.js.erb:

$("#onetime-todo").html("<%= escape_javascript(render partial: 'items', locals: { task: @one_time }) %>")
$("#onetime-done").html("<%= escape_javascript(render partial: 'done', locals: { task: @one_time_done }) %>")

$("#daily-todo").html("<%= escape_javascript(render partial: 'items', locals: { task: @daily }) %>")
$("#daily-done").html("<%= escape_javascript(render partial: 'done', locals: { task: @daily_done }) %>")

$("#weekly-todo").html("<%= escape_javascript(render partial: 'items', locals: { task: @weekly }) %>")
$("#weekly-done").html("<%= escape_javascript(render partial: 'done', locals: { task: @weekly_done }) %>")

$("#monthly-todo").html("<%= escape_javascript(render partial: 'items', locals: { task: @monthly }) %>")
$("#monthly-done").html("<%= escape_javascript(render partial: 'done', locals: { task: @monthly_done }) %>")

And finally my irritating server log:

Started DELETE "/tasks/1" for ::1 at 2017-08-14 12:59:40 -0700
Processing by TasksController#destroy as JS
  Parameters: {"id"=>"1"}
  Task Load (0.1ms)  SELECT  "tasks".* FROM "tasks" WHERE "tasks"."id" = ? LIMIT 1  [["id", 1]]
  User Load (0.1ms)  SELECT  "users".* FROM "users" WHERE "users"."id" = ?  ORDER BY "users"."id" ASC LIMIT 1  [["id", 1]]
   (0.1ms)  begin transaction
  SQL (0.4ms)  DELETE FROM "tasks" WHERE "tasks"."id" = ?  [["id", 1]]
   (0.6ms)  commit transaction
Completed 500 Internal Server Error in 15ms (ActiveRecord: 1.3ms)

ActionView::MissingTemplate - Missing template tasks/destroy, application/destroy with {:locale=>[:en], :formats=>[:js, :html], :variants=>[], :handlers=>[:erb, :builder, :raw, :ruby, :coffee]}. Searched in:
  * "/Users/lizbayardelle/Dropbox/Code/FAM/app/views"
  * "/Users/lizbayardelle/.rvm/gems/ruby-2.3.3/gems/ckeditor-4.2.2/app/views"
  * "/Users/lizbayardelle/.rvm/gems/ruby-2.3.3/gems/devise-4.2.0/app/views"
:

It bothers me that this error keeps occurring with each method. The deletion does happen, it just necessitates a refresh to appear on the page. Anyone see what's happening here?

3
  • 2
    perhaps you want delete.js.erb to be destroy.js.erb since the action is destroy, not delete. Commented Aug 14, 2017 at 20:13
  • @jvillian, well that was easy. Moron moment. Wanna write up the answer so I can pick it. Commented Aug 14, 2017 at 20:15
  • No problems. Happens to the best. Commented Aug 14, 2017 at 20:16

1 Answer 1

2

You want delete.js.erb to be destroy.js.erb since the action is destroy, not delete.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.