Simple exists? question

This has to be simple but I am not able to figure it out.

I only want the :notice data (and

tags) to show up in the
application layout if there is a notice.

The code below works if there is not a :notice (nothing shows up in
code) but gives an error if there is a notice. The error is “undefined
method `exists?’”.

<% if flash[:notice] :exists? %>

<%= flash[:notice] %>

<% end %>

Sunny

On 8/30/07, sunny beach [email protected] wrote:

<% if flash[:notice] :exists? %>

<%= flash[:notice] %>

<% end %>

Sunny

I use the following in my ApplicationController for this type of thing:

def flash_message
if flash[:notice]
%{

#{flash[:notice]}
}
elsif flash[:warning]
%{
#{flash[:warning]}
}
elsif flash[:note]
%{
#{flash[:note]}
}
elsif flash[:alert]
%{
#{flash[:alert]}
}
elsif flash[:error]
%{
#{flash[:error]}
}
end
end

then I just call

<%= flash_message %>

from within my view

Adam

There’s a syntax problem on the first line. You could do:
<% if flash[:notice].nil? %>
or simply:
<% if flash[:notice] %>

–Paul

I like Adam’s suggestion but had to put the code in the
application_helper to get it to work right.

def flash_message
if flash[:notice]
%{

#{flash[:notice]}
}
elsif flash[:warning]
%{
#{flash[:warning]}
}
elsif flash[:note]
%{
#{flash[:note]}
}
elsif flash[:alert]
%{
#{flash[:alert]}
}
elsif flash[:error]
%{
#{flash[:error]}
}
end
end

then I just call

<%= flash_message %>

from within my view

The example from Paul with .nil? does not ever return a notice … but
<% if flash[:notice] %> works just fine.

My attempt was based on my limited knowledge. I wanted to clean up the
scaffold generated code, especially if the notice was blank.

I am puzzled with this if flash[:notice] :exists? construction even
it is totally wrong.

You have If condition then … something
or you meant flash[:notice].exists?.

That is also wrong since nil do not respond to exists? Neither does
the hash or kernal.

On 8/30/07, sunny beach [email protected] wrote:

I like Adam’s suggestion but had to put the code in the
application_helper to get it to work right.

oops, I meant to say ApplicationHelper and not ApplicationController!
Glad you got it figured out.

Adam

Let’s DRY this up a bit. How about:

<% [ :message, :notice, :warning, :error ].each do |message| %>
<% if flash[message] %>


<%= flash[message] %>

<% end %>
<% end %>

On Aug 31, 1:44 pm, Josh S. [email protected]

sunny beach wrote:

I like Adam’s suggestion but had to put the code in the
application_helper to get it to work right.

def flash_message
if flash[:notice]
%{

#{flash[:notice]}
}
elsif flash[:warning]
%{
#{flash[:warning]}
}
elsif flash[:note]
%{
#{flash[:note]}
}
elsif flash[:alert]
%{
#{flash[:alert]}
}
elsif flash[:error]
%{
#{flash[:error]}
}
end
end

then I just call

<%= flash_message %>

from within my view

The example from Paul with .nil? does not ever return a notice … but
<% if flash[:notice] %> works just fine.

My attempt was based on my limited knowledge. I wanted to clean up the
scaffold generated code, especially if the notice was blank.

That code is rather ugly. You may want to submit it to DailyWTF. Try
this on instead:

flash_div

use to display specified flash messages

defaults to standard set: [:notice, :message, :warning]

example:

<%= flash_div :warning, :notice, :message %>

renders like:

Positive - successful action

Neutral - reminders, status

Negative - error, unsuccessful

action


def flash_div(*keys)
keys = [:notice, :message, :warning] if keys.blank?
keys.collect { |key| content_tag(:div, CGI.escapeHTML(flash[key]),
:class => “flash flash-#{key}”) if flash[key] }.join("\n")
end


Josh S.
http://blog.hasmanythrough.com

Hey Sunny,
Just to anwser your questions.
exists? is not a method, hence your error.
What you’ll want to do is something like

<%= flash[:notice] if flash[:notice] %>

Which is basically Ruby short hand for
<% if flash[:notice] %>
<%= flash[:notice] %>
<% end %>

You don’t need an exists? method Ruby implies it already, as does
javascript for instance. You could also use
<%= flash[:notice] unless flash[:notice].blank? %>

As blank? is a method that checks for the presence of nil

Hope this helps,
Cam

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs