1

After I am trying to display a flash message on my contact page. Here is my code

def contact_us
  UserMailer.delay.contact_email(params)
  flash[:notice] = 'Thanks for contacting us!!!!!'
  redirect_to contact_path
end

def contact
end

I tried to access the flash messages but getting a blank response.

[1] pry(#<#<Class:0x0000000aef74f0>>)> flash
=> #<ActionDispatch::Flash::FlashHash:0x0000000a633328 @discard=#<Set: {}>, @flashes={}, @now=nil>

Also tried this solution But getting same issue. Can anyone have an idea why I am getting this issue? How can I fix this issue.

Request details

Redirected to http://localhost:3000/contact

Completed 302 Found in 213ms (ActiveRecord: 46.9ms)
Started GET "/contact" for 127.0.0.1 at 2017-02-23 01:08:43 +0530

EDIT I also tried with

  • Try1

    redirect_to contact_path, notice: 'Thanks for contacting....'
    

    still getting the same issue.

  • Try 2

flash[:notice] = 'Thanks for contacting....' flash.keep(:notice) redirect_to contact_path

still getting the same issue.

Tried all the solutions from flash[:notice] doesn't work with redirect_to still getting the same error.

I am using ruby 2.0.0p643 and Rails 4.0.13.

Community
  • 1
  • 1
Natsu
  • 107
  • 10
  • what do your application logs say when you perform the request? i assume you are calling `contact_us` that get's redirected to `contact` which should display the flash message? would be helpful to put this into your question... – phoet Feb 22 '17 at 19:34
  • @phoet I have added my request details. – Natsu Feb 22 '17 at 19:40
  • 1
    Try `flash.now[:notice] = "..."` – Danny Y Feb 23 '17 at 02:19
  • @DannyY Tried your solution still getting same issue. – Natsu Feb 28 '17 at 06:08
  • If you can't solve this, try logging an issue against the Rails repository. Importantly, try to reproduce it on a new, empty Rails app, and make that one public as an example, link it in the issue. There might be a problem with rails itself. – AJFaraday Feb 28 '17 at 13:17
  • Do you have it rendering in your layout? – Avdept Feb 28 '17 at 13:51

3 Answers3

2

I got the same type of issue. When I checked the server log I got following error on the console

Can't verify CSRF token authenticity

For fixing this issue I have added following line the controller

protect_from_forgery with: :null_session
Rohit Lingayat
  • 716
  • 6
  • 23
0

Try Following.

def contact_us
  UserMailer.delay.contact_email(params)
  redirect_to contact_path
  flash[:notice] = 'Thanks for contacting us!!!!!'
end
Santosh Sharma
  • 2,114
  • 1
  • 17
  • 28
0

Add this to your application layout:

<%= flash[:notice] %>
Nicholas.V
  • 1,926
  • 2
  • 15
  • 30