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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T20:48:38+00:00 2026-06-12T20:48:38+00:00

for example, there’s a Question model that has_many :answers I’d like to show a

  • 0

for example, there’s a Question model that has_many :answers

I’d like to show a form for new answer on the questions#show ( just like stackoverflow does )

routes would be something like:

resources :questions do
  resources :answers
end

What is the best way to show the errors for an invalid record under these circumstances?

The problem is that I can’t render an action I need from within AnswersController ( since it would be questions#show ), the only way seem to be redirect_to and store errors in flash hash.

It just looks like a pretty much common scenario, I believe there should be some better way

  • 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-12T20:48:39+00:00Added an answer on June 12, 2026 at 8:48 pm

    This may be one of a few cases where it’s actually justified to add a new resourceful member route to your QuestionsController:

    resources :questions do
      post 'answer', :on => :member
    end
    

    which would recognize question/:id/answer with a POST request routed to questions#answer, allowing you keep all the logic in one controller:

    class QuestionsController < ApplicationController
      ...
      def show
        @question = Question.find(params[:id])
      end
    
      def answer
        @question = Question.find(params[:id])
    
        @answer = @question.answers.build(params[:question][:answer])
    
        if @answer.save
          # show question with newly posted answer at url /question/:id
          redirect_to @question
        else
          # show question with invalid editable answer at url /question/:id/answer
          render 'show'
        end
      end
      ...
    end
    

    Explanation: In my opinion, the decision to handle the logic in one controller as opposed to two comes down to what you consider to be the resource of interest. Normally, you would consider each model to represent a distinct resource and thus create a separate controller to handle actions related to each resource. However, when there are multiple deeply coupled models for which multiple actions (e.g. show, new, create) are handled in a single view, it might be cleaner to think of the models as forming a single resource.

    In this example, I think of the resource as a collective one consisting of both the question and its answers. Since this collective resource is uniquely identified by the question itself, I would let the question controller handle it. The show action in the questions controller already involves retrieving the collective question-answers resource, so you might think of the answer action (and potentially unanswer and reanswer actions) as the analogue of update to that collective resource.

    Thinking of the resources this way is largely a matter of design preference, and there will be trade-offs depending on the requirements.

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

Sidebar

Related Questions

Is there an example of a bitmap color replacement implementation that actually works? I'm
For example: There is a page that lists some items with colours. Some are
I will start by example. Example: There is a form with /profile/edit, and it
I have an array that has a hierarchic structure. In this example there are
For example there are lalalalaal.com that do NOT exist. Is there any way using
is there possibility to update a new field to an existing document? For example:
Im working off the seismic xml iphone example. In that example there is simply
For example there are some models class Model_1 < ActiveRecord::Base has_many :images, :as =>
Take this file as example,there are many non-extern structures like: struct list_head source_list; How
Lets say for example there are 5 records shown on screen. I want the

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.