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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T04:10:08+00:00 2026-06-13T04:10:08+00:00

I have an article model and a comment model. I currently have two separate

  • 0

I have an article model and a comment model. I currently have two separate forms to create new comments: one that allows the user to specify the name of the article their comment is for and a one underneath an article show view that creates new comments for that article. I am using form_for @comment in the first case and form_for [@article, @comment] in the second case. When a user specifies an article name as a string I convert it to an article id before saving the comment.

My routes are

resources :comments

resources :articles do
  resources :comments 
end

For the second form how can I redirect back to the article on a failed comment save (validations and errors should be displayed)? For the first form I just redirect to the homepage as that is where my first comment form is.

Also, I have a validation on the first form that the article name field cannot be empty. How can I remove this validation for the second form as the user does not need to specify an article name?

My new function in the comments_controller handles both forms. How do I determine which form is being submitted in the controller?

Thanks in advance.

  • 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-13T04:10:09+00:00Added an answer on June 13, 2026 at 4:10 am

    Actually redirecting isn’t the way to go here, I’d say. Error and validation handling in Rails usually works the way that you re-render the form in the create or update method with the validated object(s) instead of actually redirecting to the new or edit page.

    As for your problem with the two versions of comments saving, I would use form_for @comment in both versions. Dump the nested form version simulate the user’s behavior with the given article string in your form. This way you could spare a lot of if-else statements.

    As for the rendering on validation errors part, you could simply check if you have an article_id in your params (which means your creating/updating a comment through a given article) or not (which means you have the first version).

    Some code to elaborate:

    # routes.rb
    # keep the routes as they are
    resources :comments
    resources :articles
      resources :comments
    end
    
    # CommentsController.rb
    def new
      # don't use build
      @comment = Comment.new
    
      # get the article, if there is one to get
      @article = Article.find(params[:article_id]) if params[:article_id]
    
      # get all articles if there is no single one to get
      @articles = Article.all unless params[:article_id]
    end
    
    def create
      # fetch article id from title (in any case)
      # I'm assuming here
      params[:comment][:article_id] = fetch_article_id_from_title(params[:comment][:article_title])
    
      @comment = Comment.new(params[:comment])
      if @comment.save
        redirect_to everything_worked_fine_path
      else
        # render the new view of the comments and actually
        #  forget the article view. Most sites do it like this
        render action: "new"
      end
    end
    
    # form partial
    <%= form_for @comment do |f| %>
      <% if @article %>
        # there's an article to save this comment for
        <%= f.hidden_field :article_title, @article.title   # I'm assuming here
      <% else %>
        # this means there's no particular article present, so let's
        # choose from a list
        <%= f.select ... %>
      <% end %>
    <% end %>
    

    Hope this helps.

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

Sidebar

Related Questions

I have two model: User, Article A user can like or dislike many articles,
I have an article controller that deals with articles. I have a comment model
I have two models, Article and Post that both inherit from a base model
I have article model with public ICollection<Comment> Comments { get; set; } and comment
I have an article model and a user model. The user can follow that
In my app I have a article and comment model. I implemented a reply-feature,
Let's say I have 3 models (Note, Comment, Article) that I want to search
I have model Article it has field title with some text that may contain
In my Rails app, I have a model Page which represents one HTML article
I have Article model and a Comment model. Comment is created in admin.py as

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.