Topic: flash notice not showing

I've been searching the forums for the solution and have come close, but no close enough.

I'm working on setting up a blog and having comments that require validation of fields (name, email, etc.)

Currently, I have a flash notice that is set in the rb to pop when a comment is successful, but for some reason the flash notice for failure when a field is missing doesn't appear. It just redirects back to the old page with no notice (the invalid comment does not post--which is what is supposed to happen)

Here's my code, any help is appreciated!

blog_controller.rb (partial code showing)

  def comment
    #Post.find(params[:id]).comments.create(params[:comment])
    #flash[:notice] = "Added your comment"
    #redirect_to :action => "show", :id => params[:id]
    @post = Post.find(params[:id])
    @comment = @post.comments.create(params[:comment])
    if @comment.save
      flash[:notice] = 'Comment was successfully created.'
      redirect_to :action => "show", :id => params[:id]
    else
     render :action => 'show'
    end
  end

comment.rb
class Comment < ActiveRecord::Base
  belongs_to :post
  validates_presence_of   :name 
end

overall rhtml: blog.rhtml
<html>
<head>
  <title>Blog: <%= controller.action_name %></title>
  <%= stylesheet_link_tag 'scaffold' %>
</head>
<body>

<p style="color: green"><%= flash[:notice] %></p>

<%= yield  %>

</body>
</html>


blog controllers: show.rhtml
<%= render :partial => "post", :ojbect => @post %>

<%= link_to 'Edit', :action => 'edit', :id => @post %> |
<%= link_to 'Back', :action => 'list' %>

<h2>Comments</h2>
<p>Number of Comments: <%= @post.comments.count %></p>

<%= render :partial => "comment", :collection => @post.comments %>

<%= form_tag :action => "comment", :id => @post%>
<p><label for="comment_name">Name*</label><br/>
<%= text_field 'comment', 'name'  %></p>
<p><label for="comment_email">Email*</label><br/>
<%= text_field 'comment', 'email'  %></p>
<p><label for="comment_url">Website</label><br/>
<%= text_field 'comment', 'url'  %></p>

   <%= text_area "comment", "body" %><br/>
   <%= submit_tag "Comment!" %>
</form>


probably not needed to show here, but here is: _comment.rhtml
    <div>      
        <p><%= simple_format(comment.body) %></p>
        <p><small>Posted by <strong>
           <% if comment.url.blank? %>
                <%= comment.name %>
           <% else %>
                <a href='<%=comment.url%>'><%=comment.name%></a>
           <% end %>
           </strong>
           <%=  time_ago_in_words(comment.created_at) %> ago
        </small></p>
    </div>

Last edited by matthewvb (2006-11-19 16:07:42)

matthewvb

Re: flash notice not showing

its late here, so forgive me if I overlooked anything, but I can't seem to find where you show your flash messages in the templates? Maybe thats the problem?

Re: flash notice not showing

unless the flash code is not in the layout, I cant see any probs

--

Re: flash notice not showing

The flash should be displaying through this file:

overall rhtml: blog.rhtml

<html>
<head>
  <title>Blog: <%= controller.action_name %></title>
  <%= stylesheet_link_tag 'scaffold' %>
</head>
<body>

<p style="color: green"><%= flash[:notice] %></p>

<%= yield  %>

</body>
</html>


All other flash messages are displaying, just not this validation one -- which is killing me. I have a validation that is working great through another posting part of this site. 

any thoughts?

matthewvb

Re: flash notice not showing

I'm just a beginner myself, but I am pretty sure that error messages won't be in flash.

Try adding errors or error_messages_for in your show.rhtml right before you render the comment partial.

Your show.rhtml should look something like this:

<%= render :partial => "post", :ojbect => @post %>

<%= link_to 'Edit', :action => 'edit', :id => @post %> |
<%= link_to 'Back', :action => 'list' %>

<h2>Comments</h2>
<p>Number of Comments: <%= @post.comments.count %></p>

<%= error_messages_for "comment" %>

<%= render :partial => "comment", :collection => @post.comments %>

<%= form_tag :action => "comment", :id => @post%>
<p><label for="comment_name">Name*</label><br/>
<%= text_field 'comment', 'name'  %></p>
<p><label for="comment_email">Email*</label><br/>
<%= text_field 'comment', 'email'  %></p>
<p><label for="comment_url">Website</label><br/>
<%= text_field 'comment', 'url'  %></p>

   <%= text_area "comment", "body" %><br/>
   <%= submit_tag "Comment!" %>
</form>

Re: flash notice not showing

Hum, that doesn't seem to work by adding error_messages_for. That is throwing a "no implicit conversion to float from nil" error in my _comment.rhtml.

Not sure what's happening with it now. I'm going to keep searching the web/form, but let me know if you have any insight. I'll be sure to post the solution if I find it.

matthewvb

Re: flash notice not showing

<%= error_messages_for "comment" %>, is the right direction.  Can you tell what line it's throwing the error message from?

Re: flash notice not showing

It's throwing it in line 10 of the rendering of the comments:

_comment.rhtml

    <div>      
        <p><%= simple_format(comment.body) %></p>
        <p><small>Posted by <strong>
           <% if comment.url.blank? %>
                <%= comment.name %>
           <% else %>
                <a href='<%=comment.url%>'><%=comment.name%></a>
           <% end %>
           </strong>
           <%=  time_ago_in_words(comment.created_at) %> ago
        </small></p>
    </div>

matthewvb

Re: flash notice not showing

The problem is that the created_at field is populated after the record is saved.  So you'll either need to manually set the created_at field, or check for nil before you try to display it.

Re: flash notice not showing

Here's all the code that matters, just to condense it from above:

in the controller: (blog_controller.rb)

  def comment
    @post = Post.find(params[:id])
    @comment = @post.comments.create(params[:comment])
    if @comment.save
      flash[:notice] = 'Comment was successfully created.'
      redirect_to :action => "show", :id => params[:id]
    else
     render :action => 'show'
    end
  end

Views:
layouts/blog.rhtml
<html>
<head>
  <title>Blog: <%= controller.action_name %></title>
  <%= stylesheet_link_tag 'scaffold' %>
</head>
<body>

<p style="color: green"><%= flash[:notice] %></p>

<%= yield  %>

</body>
</html>


blog/show.rhtml
<%= render :partial => "post", :ojbect => @post %>

<%= link_to 'Edit', :action => 'edit', :id => @post %> |
<%= link_to 'Back', :action => 'list' %>

<h2>Comments</h2>
<p>Number of Comments: <%= @post.comments.count %></p>

<%= render :partial => "comment", :collection => @post.comments %>

<%= start_form_tag :action => "comment", :id => @post%>
  <%= render :partial => 'comment_form' %>
   <%= submit_tag "Comment!" %>
<%= end_form_tag %>


blog/

matthewvb

Re: flash notice not showing

Brilliant! That solved the problem. Thanks for your help thabenksta and everyone else!

matthewvb