SitePoint Sponsor

User Tag List

Results 1 to 6 of 6

Hybrid View

  1. #1
    SitePoint Evangelist
    Join Date
    Jun 2001
    Location
    Houston, Texas, USA
    Posts
    559
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    grabbing flashed errors

    What's the best way to grab the flash errors and print them in a view?

    I found this code on the web, but I am getting a compile error:

    <% unless my_model.errors.empty? %>
    <p>There were errors saving the data!</p>
    <ul>
    <% my_model.errors.full_messages.each |message| %>
    <li><%= message %></li>
    <% end %>
    </ul>
    <% end %>


    Here are the error messages:

    Showing app/views/feeds/_form.rhtml where line #4 raised:

    compile error
    /Users/robertgremillion/workspace/reminderfeed/public/../config/../app/views/feeds/_form.rhtml:4: parse error, unexpected ';'
    _erbout.concat " "; @feed.errors.full_messages.each |message| ; _erbout.concat "\n"
    ^
    /Users/robertgremillion/workspace/reminderfeed/public/../config/../app/views/feeds/_form.rhtml:46: parse error, unexpected kEND, expecting $

    Extracted source (around line #4):

    1: <% unless @feed.errors.empty? %>
    2: <p>There were errors saving the data!</p>
    3: <ul>
    4: <% @feed.errors.full_messages.each |message| %>
    5: <li><%= message %></li>
    6: <% end %>
    7: </ul>


    What am I doing wrong?

    Thanks!

  2. #2
    SitePoint Evangelist
    Join Date
    Jun 2004
    Location
    California
    Posts
    440
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Code:
         <% my_model.errors.full_messages.each |message| %>
    Should be (you forgot the "do")

    Code:
         <% my_model.errors.full_messages.each do |message| %>
    Happy switcher to OS X running on a MacBook Pro.

    Zend Certified Engineer

  3. #3
    SitePoint Guru silver trophy Luke Redpath's Avatar
    Join Date
    Mar 2003
    Location
    London
    Posts
    794
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I use a standard of flash[:notice], flash[:error] and flash[:warning], and then I use these helpers in my application_helper.rb to display my flashes - it will handle strings in the above and will also print out a list of activerecord errors if you've assigned them to flash[:error]:

    Code:
    def display_standard_flashes(message = 'There were some problems with your submission:')
        if flash[:notice]
          flash_to_display, level = flash[:notice], 'notice'
        elsif flash[:warning]
          flash_to_display, level = flash[:warning], 'warning'
        elsif flash[:error]
          level = 'error'
          if flash[:error].instance_of? ActiveRecord::Errors
            flash_to_display = message
            flash_to_display << activerecord_error_list(flash[:error])
          else
            flash_to_display = flash[:error]
          end
        else
          return
        end
        content_tag 'div', flash_to_display, :class => "flash #{level}"
      end
    
    def activerecord_error_list(errors)
        error_list = '<ul class="error_list">'
        error_list << errors.collect do |e, m|
          "<li>#{e.humanize unless e == "base"} #{m}</li>"
        end.to_s << '</ul>'
        error_list
      end

  4. #4
    SitePoint Evangelist
    Join Date
    Jun 2001
    Location
    Houston, Texas, USA
    Posts
    559
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for the replies. I was looking for a way to grab the "flashed" errors so I could format the error messages myself. I was missing the 'do'.

    At first I was thinking there was a flash hash of messages? Is there such a thing? Or do all of the messages just get added to the model object?

  5. #5
    SitePoint Guru silver trophy Luke Redpath's Avatar
    Join Date
    Mar 2003
    Location
    London
    Posts
    794
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Validation errors are part of your model - you have to store them in the flash hash yourself.

  6. #6
    SitePoint Enthusiast
    Join Date
    Feb 2006
    Posts
    28
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Code:
    <% for name in [:info, :warning, :error] %>
      <% if @flash[name] %>
        <p class="<%= "#{name}-message message" %>" >
          <%= @flash[name] %> 
          <% if request.xhr? %>
            <a href="#" onclick="Element.remove(this.parentNode); return false;">Close</a>
          <% end %>
        </p>
      <% end %>
    <% end %>
    easy peasy


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •