title: Redirecting users after destroy
body: <p>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:</p>
  
  <pre><code>%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 =&gt; 'Are you sure?', :method =&gt; :delete}
  </code></pre>
  
  <p>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?</p>
  
  <p>I know that there's a <code>redirect_to</code> 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.</p>
  
  <p>I tried passing something like :question_id in link_to as:</p>
  
  <pre><code>#{link_to 'Delete my answer', a, :confirm =&gt; 'Are you sure?', :question_id =&gt; @question.id, :method =&gt; :delete}
  </code></pre>
  
  <p>In AnswersController#destroy:</p>
  
  <pre><code>  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
  </code></pre>
  
  <p>The :question_id information is not passed to the destroy method, so I get this error: </p>
  
  <pre><code>Couldn't find Question without an ID
  </code></pre>
  
  <p>To confirm, I added a <code>puts</code> call before <code>Question.find</code>, and it returned <code>nil</code>.</p>
  
  <p>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:</p>
  
  <pre><code>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".
  </code></pre>
  
  <p>I'm not sure how to address is error. Where should I put the <code>return</code>?</p>
  
  <p>Any help would be appreciated!!</p>