0

I want to redirect the user to the event_calendar_path when it goes too many months ahead using the link "next month" showing up a flash message saying "You cannot go more than 6 months ahead".

I would like to display it as an error message, but it doesn't show. I tried with :notice, :info, :error, but only :notice gets displayed actually.

This is what I have in my application.html.haml file:

- flash.each do |key, msg|
  - if key == :notice
    .alert.alert-success= msg
  - elsif key == :error
    .alert.alert-error= msg
  - elsif key == :info
    .alert.alert-info= msg
  - else
    .alert.alert-block= msg

And this is what I'm doing to display the flash message:

if @current_month > Date.today.month + 6
  flash[:error] = "You cannot go more than 6 months ahead"
  redirect_to event_calendar_path @event_calendar, :group_id => @group.id
  return
end

If i replace :error with notice it works, otherwise I get no message. Before I was using the short form :notice => "Message", but then I changed it to make sure it was not an issue using that form to display messages, but I'm still not able to display it.

What can be the cause for this behaviour? How can I fix it?

Thanks.

3 Answers3

0

Look at:

http://guides.rubyonrails.org/action_controller_overview.html#the-flash

flash.now

Roger
  • 7,535
  • 5
  • 41
  • 63
  • Sorry but I can't see how this is going to solve my problem. I'm not rendering the view directly in the same request, but rather I'm redirecting the user to another path, where I want to show the message. I should not even need `Flash.keep` because there's only one request next (the redirected view), so it should work as expected, am I wrong? –  May 17 '12 at 11:33
  • did you try: redirect_to root_url, :error => "You cannot go more than 6 months ahead" – Roger May 17 '12 at 14:30
0

Are you sure that you don't extracting your :error/:info keys from flash elsewhere in controller's before filter or a layout maybe?

ABrukish
  • 1,432
  • 1
  • 9
  • 9
0

Can you try giving the same html class attribute for both? Maybe your clients-side logic hides the error message?

So, instead of:

- if key == :notice
  .alert.alert-success= msg
- elsif key == :error
  .alert.alert-error= msg

try with:

- if key == :notice
  .alert.alert-success= msg
- elsif key == :error
  .alert.alert-success= msg
Salil
  • 9,534
  • 9
  • 42
  • 56