chrishorton
chrishorton

Reputation: 50

Upon clicking delete, rails refreshes page but does nothing

I am writing a rails app for planning school assignments.

I have been experiencing a problem upon clicking the delete button on the web page it just refreshes the page but doesn't delete anything from the page.

Here is my destroy action from assignment_controller along with assignment_params.

def destroy 
    @assignment = Assignment.find(params[:id])
    @assignment.destroy

    redirect_to assignments_path
end

private

def assignment_params
    params.require(:assignment).permit(:name)
end

And this is my index.html.erb

<h1>Listing of Assignments</h1>

<% @assignment.each do |x| %>
    <h2><%= x.name %></h2>
    <%= link_to "Delete", assignments_path(@assignment),
        method: :delete,
        data: { confirm: "Are you sure?" } %>
<% end %>

I'm not sure if the problem is with the route but here is my routes.rb just in case.

Rails.application.routes.draw do

devise_for :users
resources :assignments

root 'home#index'

end

Thank you!

Upvotes: 0

Views: 130

Answers (2)

user5245636
user5245636

Reputation:

Instead of:

<%= link_to "Delete", assignments_path(@assignment),
        method: :delete,
        data: { confirm: "Are you sure?" } %>

Try this:

<%= link_to "Delete", assignment_path(x),
        method: :delete,
        data: { confirm: "Are you sure?" } %>

You must pass the x not @assignment because you have been passed it in a loop. Also, change: assignments_path to assignment_path.

Upvotes: 1

Pavan
Pavan

Reputation: 33542

You have to change your link for delete to below

<%= link_to "Delete", x, method: :delete, data: { confirm: "Are you sure?" } %>

Upvotes: 1

Related Questions