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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T17:09:16+00:00 2026-05-12T17:09:16+00:00

I have a blog model with has many model comments relationship. So my form

  • 0

I have a blog model with has many model comments relationship.

So my form looks something like this located in the blog show view:

<% form_for [@blog, @comment] do |f| -%>
  <%= f.error_messages %>

  <%= f.label :message, "Add your message" %>
  <%= f.text_area :message %>

  <%= f.submit "Submit" %>
<% end -%>  

All very straightforward.

I have one action in the comment controller and that is a create action.

def create
  @comment = Comment.new(params[:comment])

  respond_to do |format|
    if @comment.save
      flash[:success] = "Thank you for your comment"
      format.html { redirect_to :back }
    else
      format.html { render :action => "new" }
    end
  end
end

My problem is that when a validation error occurs in the comment model then I will end up rendering the new comment view which doesn’t exist.
I want to render the blog show view along with the error messages.
If I try to redirect back or render the blog show view template then the users comments will end up deleted as state is not preserved between them.
Can anyone tell me what the conventional solution is to this problem? Thanks.

  • 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-12T17:09:17+00:00Added an answer on May 12, 2026 at 5:09 pm

    The solution is to have your blog accept_nested_attributes_for :comments.

    By only modifying comments through the blog controller you will ensure that the you return to the blog view with validations if something goes wrong.

    Changes you need to make to make it work.

    class Blog < ActiveRecord::Base
      ...
      accepts_nested_attributes_for :comments
    end
    

    Blog/show view

    <% form_for @blog do |f| -%>
      <%= f.error_messages %>
      <% new_comments @blog.comments.select{&:new_record?} %>
      <%= f.fields_for :comments, new_comments.empty? ? @blog.comments.build : new_comments do |c| %>
      <%= c.label :message, "Add your message" %>
      <%= c.text_area :message %>
      <%=end%>
    
      <%= f.submit "Submit" %>
    <% end -%>
    

    If there isn’t a update method in your Blog controller you will need to add on to make it work. But I believe this isn’t necessary with restful routes as rails will take the common action. However, that might also require you to have a blog edit view that is nearly identical to the show view. Instead use this code in your controller.

    class BlogsController < ApplicationController
      def update
        @blog = Blog.find(params[:id])
        if @blog.update_attributes(params[:blog])
          redirect_to @blog
        else
          render :action => show
        end
      end
    end
    

    If you’re using attr_accessible for the Blog model you will have to add :comments to the list.

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

Sidebar

Related Questions

No related questions found

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.