This has to be simple but I am not able to figure it out. I only want the :notice data (and <p> 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? %> <p><%= flash[:notice] %></p> <% end %> Sunny --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
On 8/30/07, sunny beach <sunnybeach1999-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > This has to be simple but I am not able to figure it out. > > I only want the :notice data (and <p> 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? %> > <p><%= flash[:notice] %></p> > <% end %> > > Sunny >I use the following in my ApplicationController for this type of thing: def flash_message if flash[:notice] %{<div class="notice">#{flash[:notice]}</div>} elsif flash[:warning] %{<div class="warning">#{flash[:warning]}</div>} elsif flash[:note] %{<div class="note">#{flash[:note]}</div>} elsif flash[:alert] %{<div class="alert">#{flash[:alert]}</div>} elsif flash[:error] %{<div class="error">#{flash[:error]}</div>} end end then I just call <%= flash_message %> from within my view Adam --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
There''s a syntax problem on the first line. You could do: <% if flash[:notice].nil? %> or simply: <% if flash[:notice] %> --Paul On Aug 30, 5:10 pm, "sunny beach" <sunnybeach1...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> This has to be simple but I am not able to figure it out. > > I only want the :notice data (and <p> 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? %> > <p><%= flash[:notice] %></p> > <% end %> > > Sunny--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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 Aug 30, 11:23 pm, "Paul E. G. Lynch" <plynch...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> There''s a syntax problem on the first line. You could do: > <% if flash[:notice].nil? %> > or simply: > <% if flash[:notice] %> > > --Paul > > On Aug 30, 5:10 pm, "sunny beach" <sunnybeach1...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > This has to be simple but I am not able to figure it out. > > > I only want the :notice data (and <p> 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? %> > > <p><%= flash[:notice] %></p> > > <% end %> > > > Sunny--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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]
%{<div class="notice">#{flash[:notice]}</div>}
elsif flash[:warning]
%{<div class="warning">#{flash[:warning]}</div>}
elsif flash[:note]
%{<div class="note">#{flash[:note]}</div>}
elsif flash[:alert]
%{<div class="alert">#{flash[:alert]}</div>}
elsif flash[:error]
%{<div class="error">#{flash[:error]}</div>}
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.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---
On 8/30/07, sunny beach <sunnybeach1999-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> 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 --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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] > %{<div class="notice">#{flash[:notice]}</div>} > elsif flash[:warning] > %{<div class="warning">#{flash[:warning]}</div>} > elsif flash[:note] > %{<div class="note">#{flash[:note]}</div>} > elsif flash[:alert] > %{<div class="alert">#{flash[:alert]}</div>} > elsif flash[:error] > %{<div class="error">#{flash[:error]}</div>} > 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: # <div class="flash flash-notice">Positive - successful action</div> # <div class="flash flash-message">Neutral - reminders, status</div> # <div class="flash flash-warning">Negative - error, unsuccessful action</div> 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 Susser http://blog.hasmanythrough.com -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
rein.henrichs-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org
2007-Sep-03 04:52 UTC
Re: simple exists? question
Let''s DRY this up a bit. How about:
<% [ :message, :notice, :warning, :error ].each do |message| %>
<% if flash[message] %>
<div class="<%= message.to_s %>"
id="flash">
<%= flash[message] %>
</div>
<% end %>
<% end %>
On Aug 31, 1:44 pm, Josh Susser
<rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org>
wrote:> 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]
> > %{<div
class="notice">#{flash[:notice]}</div>}
> > elsif flash[:warning]
> > %{<div
class="warning">#{flash[:warning]}</div>}
> > elsif flash[:note]
> > %{<div class="note">#{flash[:note]}</div>}
> > elsif flash[:alert]
> > %{<div
class="alert">#{flash[:alert]}</div>}
> > elsif flash[:error]
> > %{<div
class="error">#{flash[:error]}</div>}
> > 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:
> # <div class="flash flash-notice">Positive - successful
action</div>
> # <div class="flash flash-message">Neutral - reminders,
status</div>
> # <div class="flash flash-warning">Negative - error,
unsuccessful
> action</div>
> 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 Susserhttp://blog.hasmanythrough.com
> --
> Posted viahttp://www.ruby-forum.com/.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---
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 --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---