-1
# users_controller.rb

def create
    @user = User.new(name: params[:name],
      email: params[:email],
      password: params[:password],
      password_confirmation: params[:password_confirmation])
    if @user.save
      # successfully saved
      flash[:success] = "Sign up success. WELCOME!" # problem
      redirect_to user_path(@user.id)
    else
      @error_messages = @user.errors.full_messages
      flash[:danger] = "Sign up failed."
      render 'new'
    end
    debugger
end

Hi, I'm using Rails 5.2.0, ruby 2.4.1 and working on AWS cloud9 environment. This is the code about controller for sign-up page. As you see, the controller takes the information from HTML form and make a @user variable, and save it when it meets the condition I made in app/models/user.rb. I used debugger to test if there is flash[:success] nicely, and debugger says

(byebug) flash
#<ActionDispatch::Flash::FlashHash:0x007f90180a8fe8 @discard=#<Set: {}>, @flashes={"success"=>"Sign up success. WELCOME!"}, @now=nil>

so I can know there is a flash message.

I want to show a sign up success message on that redirected page. But It doesn't work as I expected.

<!-- This is the page when sign up succeed, using semantic-ui -->

<% flash.each do |key, value| %>
    <% if key=='success' %>
        <div class="ui positive message">
            <i class="close icon"></i>
            <div class="header"><%= value %></div>
            <p>HALLO</p>
        </div>
    <% end %>
<% end %>

This code render nothing on the page, What's gone wrong with this code? I've already spent over 3 hours to figure out the solution and also read about similar cases on stackoverflow but it doesn't solved my problem.

Am I missing something?

halfelf
  • 9,737
  • 13
  • 54
  • 63
Mr. Kidney
  • 43
  • 5
  • You checked adding debugger in UI.. ? Where you put the code for Flash message. – praga2050 Sep 02 '18 at 05:35
  • I added debugger in UI as you said – Mr. Kidney Sep 02 '18 at 05:41
  • <%= debug(flash) if Rails.env.development? %> in the page for the flash message – Mr. Kidney Sep 02 '18 at 05:41
  • And that says: --- !ruby/object:ActionDispatch::Flash::FlashHash discard: !ruby/object:Set hash: {} flashes: {} now: – Mr. Kidney Sep 02 '18 at 05:41
  • You hash and flashes are empty..... See this is my debug result in my UI --- !ruby/object:ActionDispatch::Flash::FlashHash discard: !ruby/object:Set hash: success: true flashes: success: Sign up success. WELCOME! now: – praga2050 Sep 02 '18 at 05:43
  • then can I think this as a problem of AWS cloud9? – Mr. Kidney Sep 02 '18 at 05:46
  • you are testing in your local only right? and flash message code must be in your application.html.erb. See if my below answer helps you.. also attached screenshot – praga2050 Sep 02 '18 at 05:51
  • flash message must be in my application.html.erb ? Then It should be the problem. Because I embeded those erb code in show.html.erb, which is my user-profile page. I'll test it. – Mr. Kidney Sep 02 '18 at 05:54
  • Still not working... I'm about to die – Mr. Kidney Sep 02 '18 at 05:59
  • After adding in application.html.erb also you see hash & flashes empty? – praga2050 Sep 02 '18 at 06:05
  • yes. here is my try: https://imgur.com/a/ZyvQ1dU / Code in my application.html.erb: https://imgur.com/a/mLx6U2r – Mr. Kidney Sep 02 '18 at 06:11
  • I have the same problem, flash.each is in my application.html.er since the beginning and other pages flashes work fine, only when redirecting from a validation fail I cannot see any flash... redirect_to some_path(@something), notice: "message" WORKS, Please help us (It is very frustrating) – Luis Flores Jan 19 '21 at 19:37

1 Answers1

-2

I added the flash code to my application.html.erb

<%= debug(flash) if Rails.env.development? %>
<% flash.each do |key, value| %>
  <% if key=='success' %>
    <div class="alert alert-success" role="alert">
      <i class="close icon"></i>
      <div class="header"><%= value %></div>
      <p>HALLO</p>
    </div>
  <% end %>
<% end %>

Screenshot is here enter image description here

Pardeep Dhingra
  • 3,916
  • 7
  • 30
  • 56
praga2050
  • 729
  • 9
  • 18