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

  • Home
  • SEARCH
  • 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 7798851
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T00:07:06+00:00 2026-06-02T00:07:06+00:00

I am trying to write my delete method in my comment controller. My comment

  • 0

I am trying to write my delete method in my comment controller. My comment model has polymorphic associations with other models but in this case, we’ll just focus on trips. In other words, @trip = @commentable.

The comment gets deleted just fine, but I keep getting the error ActionController::ActionControllerError in CommentsController#destroy: Cannot redirect to nil! when I redirect_to @commentable which would be the trip that the comment belonged to.

I also redirected to @commentable in my create action (comment controller) and that works just fine when the user creates a new comment.

Any tips?

view (trips/show.html.erb)

<% if !@commentable.comments.empty? %>
  <% @commentable.comments.each do |comment| %>
    <!-- Content -->
    <%= link_to comment, :method => :delete do %> delete <% end %>
  <% end %> 
<% end %>

comment form that works for create action

<%= form_for [@commentable, Comment.new] do |f| %>
  <%= f.text_area :content %>
  <div id="comment_submit_button"><%= f.submit "Comment" %></div>
<% end %>

trips_controller.rb

def show
  @trip = @commentable = Trip.find(params[:id])
  @comments = Comment.all
end

comments_controller.rb

 def create
   @commentable = find_commentable
   @comment = @commentable.comments.build(params[:comment])
   @comment.user_id = current_user.id

   if @comment.save
     redirect_to @commentable
   end
 end

 def destroy
  # @commentable = find_commentable    this line was wrong
   @comment = Comment.find(params[:id]) 
   @commentable = @comment.commentable #this line fixed it
   if @comment.destroy
     redirect_to @commentable
   end
 end


def find_commentable
  params.each do |name, value|
    if name =~ /(.+)_id$/
      return $1.classify.constantize.find(value)
    end
  end
 nil
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-02T00:07:08+00:00Added an answer on June 2, 2026 at 12:07 am

    Figured out the solution. Will post solution in code above.

    def destroy
      @comment = Comment.find(params[:id])
      @commentable = @comment.commentable
      if @comment.destroy
        redirect_to @commentable
      end
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm sure I'm going about this wrong but I'm trying to write a simple
I'm trying to write some SQL that will delete files of type '.7z' that
I'm trying to write a macro to delete all rows that have True in
I am trying to write a C++ program to delete the shared memory segments.
I'm trying to write a windows batch file that can delete files from subdirectories.
I am trying write a function that generates simulated data but if the simulated
Trying to write out syslog entries containing strings but they don't register. // person.name
I am trying to write a method that gets files from a folder, orders
I have a BufferedImage I'm trying to write to a jpeg file, but my
I am trying to write an antlr script where in, rule1 has a subrule

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.