Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7842115
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T16:14:39+00:00 2026-06-02T16:14:39+00:00

I am trying to simply create a button that will delete a contact from

  • 0

I am trying to simply create a button that will delete a contact from a list of contacts.

At the moment I have the following setup:

Contacts Controller

def destroy
 @user.contacts.delete(params[:contact])
 @contact.delete
end

View

<tbody>
<% @contacts.each do |contact| %>
    <tr>    
        <td><%= contact.name %></td>
        <td><%= contact.company %></td>
        <td><%= contact.email %></td>
        <td><%= contact.phone %></td>
        <td><%= contact.mobile %></td>
        <td><%= button_to 'Delete', contact, :method => :delete %></td>
    </tr>
<% end %>

Routes

controller :contact do
 get "newcontact" => "contact#new"
 get "index" => "contact#index"
 delete "delete" => "contact#destroy"
end

I have read online that using button_to is the preferred method but at the moment I am getting the following error:

undefined method `contacts' for nil:NilClass

It would be great to get any feedback that might help me fix this.

Thanks in advance
Tom

For anyone looking for the final answer on this it was

   def destroy
     contact = current_user.contacts.find(params[:id])
     contact.destroy
     redirect_to index_path
   end

In my controller and the following in my view

<%= button_to 'Delete', contact, :method => :delete %>

Thanks
Tom

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T16:14:40+00:00Added an answer on June 2, 2026 at 4:14 pm

    If you are goin to use @user you need to set @user in your delete method or else it will be nil – hence the error message

    The other issue is that the id of the contact is params[:id] and not params[:contact]

    I’d just do

    contact = current_user.contacts.find(params[:id])
    contact.destroy
    

    You definitely want to be deleting the contact via the user or else people can delete other users’ contacts. This deletes the contact and removes it from the association – you don’t need to do anything else

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to create a button that will simply link back to the context
I'm trying to create a simple button that deletes a row from the database.
I'm trying to create a function that will simply allow me to pass an
I'm trying to create a simple login system that will have the forms and
I'm trying to create a button that, when clicked will cycle ad nauseum through
I have been trying to create a simple application that will let the user
what i'm trying to do this this. Simply create a C# windows app that
I'm trying to create an app for Android that simply sends a command to
I am trying to create a very simple chat window that simply has the
I am trying to create a simple program that will allow the user to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.