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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T14:31:53+00:00 2026-05-14T14:31:53+00:00

I have built a blog using Ruby on Rails. New to both. I am

  • 0

I have built a blog using Ruby on Rails. New to both. I am implementing AJAX pretty effectively until I get to the error handling portion.

I allow for comments on posts and do this by rendering a comment partial and remote form in the /views/posts/show.html.erb page. Upon successful save of a comment the show page is updated using views/comments/create.js.rjs and displays a flash notice.

I am simply trying to flash a notice when it doesn’t save. Searched around and worked this a bit on my own. Can’t get it to fly. Here is my code:

/views/posts/show.html.erb

<div id="comments">

    <%= render :partial => @post.comments %>
    <div id="notice"><%= flash[:notice] %></div> 
</div>

<% remote_form_for [@post, Comment.new] do |f| %>

    <p>
        <%= f.label :body, "New Comment" %><br/>
        <%= f.text_area (:body, :class => "textarea") %>
    </p>
    <p>
        <%= f.label :name, "Name" %><br/>
        <%= f.text_field (:name, :class => "textfield") %>
    </p>
    <p>
        <%= f.label :email, "Email" %><br/>
        <%= f.text_field (:email, :class => "textfield") %>
    </p>
    <p><%= f.submit "Add Comment" %></p>
<% end %>

/views/comments/_comment.html.erb

<% div_for comment do %>
<div id="comment-wrapper">

        <%  if admin? %>
        <div id="comment-destroy"><%=link_to_remote "X", :url => [@post, comment], :method => :delete %></div>
        <% end %>
        <%= h(comment.body) %><br/><br/>
        <div class="small">Posted <%= time_ago_in_words(comment.created_at) %> ago by <%= h(comment.name) %> 
        <%  if admin? %>
         | <%= h(comment.email) %>

        <% end %></div>

    </div>
<% end %>

/views/comments/create.js.rjs

page.insert_html :bottom, :comments, :partial => @comment
page[@comment].visual_effect :highlight 
page[:new_comment].reset
page.replace_html :notice, flash[:notice]
flash.discard

CommentsController#create

 def create
   @post = Post.find(params[:post_id])
   @comment = @post.comments.create!(params[:comment])

   respond_to do |format|
     if @comment.save
       flash[:notice] = "Thanks for adding this comment" 
       format.html { redirect_to @post }
       format.js
     else
       flash[:notice] = "Make sure you include your name and a valid email address" 
       format.html { redirect_to @post }
       format.js

   end
 end
end 
  • 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-14T14:31:53+00:00Added an answer on May 14, 2026 at 2:31 pm

    The create! is a problem because create! raises an exception when the model is invalid, meaning the respond_to code below it will not execute.

    Like Corey says, replace create! with build. Your strategy of displaying the flash notice via rjs may well work then.

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

Sidebar

Related Questions

I have built a very simple blog application using Ruby on Rails. New to
I have built a blog application using Ruby on Rails. In the application I
I am new to rails so go easy on me. I have built a
I have built a web page which contains a Crystal Report built using the
When using scaffolding in Ruby on Rails to build my controller, the scaffold is
I'm using IIS6 (unfortunatly) and have built an ASP.Net MVC app; IIS is set
Lately I have seen a lot of blog posts concerning how to build loosely
I have built an MSI that I would like to deploy, and update frequently.
I have built a simple C#.Net app on a M/C with only .Net FX
I have built an application in C# that I would like to be optimized

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.