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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T19:23:54+00:00 2026-05-14T19:23:54+00:00

I have 3 models: Questions, Answers, and Profiles (I know, it should be called

  • 0

I have 3 models: Questions, Answers, and Profiles (I know, it should be called “Users”). When you view a question Q, I query the database for the answers to Q. (They are linked by id.) In the view, the current user has the option to delete his answer by clicking on the destroy link displayed next to his answer:

%table
  %tr
    %td
      Answers:
  - @answers.each do |a|
    %tr
      %td
        - @provider = Profile.find(a.provider)
        %i
          #{h @provider.username} said:
        %br
        #{h a.description}
      %td
        = link_to 'View full answer', a
      %td
        - if a.provider == @profile.id
          #{link_to 'Delete my answer', a, :confirm => 'Are you sure?', :method => :delete}

The problem is that when the user clicks on the destroy link, it redirects to the /answers/index. I want it to redirect to /questions/Q. What’s the best way to do this?

I know that there’s a redirect_to method, but I don’t know how to implement it when I want to redirect to an action for a different controller. It also needs to remember the question from which the answer is being deleted.

I tried passing something like :question_id in link_to as:

#{link_to 'Delete my answer', a, :confirm => 'Are you sure?', :question_id => @question.id, :method => :delete}

In AnswersController#destroy:

  def destroy
    @answer = Answer.find(params[:id])
    @answer.destroy

    respond_to do |format|
      format.html { redirect_to(answers_url) }
      format.xml  { head :ok }
    end

    @question = Question.find(params[:question_id])
    redirect_to question_path(@question)
  end

The :question_id information is not passed to the destroy method, so I get this error:

Couldn't find Question without an ID

To confirm, I added a puts call before Question.find, and it returned nil.

I have also tried storing the question id in the session information. AnswersController#destroy changes to call session[:question_id] instead of params[:question_id]. But, I get a different error:

Render and/or redirect were called multiple times in this action. Please note that you may only call render OR redirect, and at most once per action. Also note that neither redirect nor render terminate execution of the action, so if you want to exit an action after redirecting, you need to do something like "redirect_to(...) and return".

I’m not sure how to address is error. Where should I put the return?

Any help would be appreciated!!

  • 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-05-14T19:23:55+00:00Added an answer on May 14, 2026 at 7:23 pm

    In the action that defines the destroy behaviour, you simply add a redirect_to on completion.

    redirect_to can use any of you standard url/rout helper methods:

    Assuming you have a Questions controller and associated routs and actions, you could use:

    redirect_to question_path(@question)
    

    In the edited case above, you need to ensure the question_id is passed correctly:

    link_to ‘Delete my answer’, answer_path(a, :question_id => @question.id), :confirm => ‘Are you sure?’, :method => :delete

    link_to’s second param is the URL in question. You can check the generated HTML to confirm the data is being rendered as you expect.

    There are also some other options here:

    Depending on how your routes and relationships are set up, you may be able to make Answer a nested route that lives inside the Question context. Which would give you a set of question_answer_path helpers.

    If the Answer belongs to the Question, you could actually just use answer.question_id in the destroy action and save the hassle 😛

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

Sidebar

Related Questions

I have two model: class Questions(models.Model): question = models.TextField(verbose_name='Question') def get_answers(self): query = self.answers.select_related()
A simple question: I have a Model-View-Controller setup, with Models accessing a SQL database.
I have two models questions and answers. and a question has_many answers. How can
I know similar questions have been asked but they all have different answers and
I have two models (questions and answers) that slightly follow the popular Railscasts :
I have read similar questions but they talk of AI models. What I want
HI, I have a Test model, which has_many questions, and Question, which has_many answers...
I have two models: call them questions and answers: class FacetQuestion(models.Model): the_question = models.CharField(max_length=50)
I have two tables: answers{..., question_id} and questions{..., answer_id} . In the models I
I have a django app with models as follows: A Question model An Answer

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.