Justin
Justin

Reputation: 7

Delete Button Not Working - Not Sure What is Wrong

all,

I tried this:

            <h9>Thursday</h9>
            <% @thursday.each do |chorelist| %><br>
                <p2><%= chorelist.name %></p2>
                <%= button_to "delete", chorelists_destroy_path(:id, chorelist.id) %>
            <% end %>

It won't do the trick, however.

As you can see, I am trying to add a delete button with each iteration. What am I missing here?

I'm still new to Rails, so any help will be most appreciated.

Addendum: I received this error message when using that latter method:

"Couldn't find Chorelist with 'id'={:name=>"Sweep Floors", :day=>"Sunday", :user_id=>2}"

I checked the console, and those parameters are all there.

  class SaveListController < ApplicationController

  before_filter :authenticate_user!

  def index
  @chorelist = Chorelist.create(user_id: params[:user_id], chore_id:     params[:chore_id], day: params[:day], name: params[:chore_name])

redirect_to pick_chores_path

end


def display
@chorelist = Chorelist.all

@monday = @chorelist.where("day = 'Monday'", user_id: current_user.id)
@tuesday = @chorelist.where("day = 'Tuesday'", user_id: current_user.id)
@wednesday = @chorelist.where("day = 'Wednesday'", user_id: current_user.id)
@thursday = @chorelist.where("day = 'Thursday'", user_id: current_user.id)
@friday = @chorelist.where("day = 'Friday'", user_id: current_user.id)
@saturday = @chorelist.where("day = 'Saturday'", user_id: current_user.id)
@sunday = @chorelist.where("day = 'Sunday'", user_id: current_user.id)

  end

 def destroy
 @chorelist = Chorelist.find(id: params[:id])
 @chorelist.destroy

redirect_to show_chores_path

end

end

Rails.application.routes.draw do

resources :chorelists

get 'about' => 'welcome#about'

get 'contact' => 'welcome#contact'

root 'welcome#index'

get 'about' => 'welcome#about'

get 'pick_chores' => 'pick_chores#index'

post 'save_list' => 'save_list#index'

get 'show_chores' => 'save_list#display'

post 'chorelists_destroy' => 'save_list#destroy'

resources :chores devise_for :users

end

Upvotes: 0

Views: 584

Answers (1)

jamesjaya
jamesjaya

Reputation: 665

Change :id, chorelist.id to id: chorelist.id

<h9>Thursday</h9>
<% @thursday.each do |chorelist| %><br>
    <p2><%= chorelist.name %></p2>
    <%= button_to "delete", chorelists_destroy_path(id: chorelist.id) %>
<% end %>

On controller

def destroy
    @chorelist = Chorelist.find(params[:id])
    @chorelist.destroy
    # do your things
end

Upvotes: 1

Related Questions