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 8210071
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T09:56:52+00:00 2026-06-07T09:56:52+00:00

When the user is logged in, only the user who create the record can

  • 0

When the user is logged in, only the user who create the record can destroy his own record.
What should I add to the code below??

  def destroy

    @topic = Topic.find(params[:id])
    @topic.destroy
    flash[:notice] = "topic deleted!"

  end
  • 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-07T09:56:54+00:00Added an answer on June 7, 2026 at 9:56 am

    What you are looking for is not really devise but a authorization solution like CanCan.

    Devise can only authenticate users and verify that they are logged in and active. What you need is a way to determine if the user has the right to delete this topic or not.

    You can of course roll your own like this:

      def destroy
        @topic = Topic.find(params[:id])
        if @topic.user_id == current_user.id
          @topic.destroy
          flash[:notice] = "topic deleted!"
        else
          flash[:error] = "not allowed"
        end
      end
    

    (The code assumes you have a belongs_to :creator, :class_name => :user association set up in your Topic.. But you get the idea).

    But using something like CanCan will make your life a whole lot easier and would reduce the code to something like this:

      def destroy
        @topic = Topic.find(params[:id])
        authorize! :destroy, @topic
        @topic.destroy
        flash[:notice] = "topic deleted!"
      end
    

    With your ability file (See defining abilities) set up like this:

    can :manage, Topic, :owner_id => user.id
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to do some processing only after the user has successfully logged in
Should I be writing assertTrue(User logged in, user.isLoggedIn()); or assertTrue(User is not logged in,
I've got some nifty code that on user login, finds any other user logged
After the user logged in , I need a page where each registered user
What's best way to keep user logged on a PHP-powered site until he closes
What would be the best practice to keep the user logged in if he
I made a login button and want to check if the user logged in
I have the following class which handles my user logged in / logged out
I have a JSF session scope bean and I'm keeping current user(logged in) information
I'm using CodeIgniter 2.1 I use CodeIgniter's session to handle whether a user logged

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.