Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Flash returns the same message twice #196

Closed
greggilbert opened this issue Dec 20, 2016 · 11 comments
Closed

Flash returns the same message twice #196

greggilbert opened this issue Dec 20, 2016 · 11 comments
Assignees
Milestone

Comments

@greggilbert
Copy link

I'm having a weird issue using flash with controller 0.8.1, where I get the same message twice. I'm POSTing to save.rb, saving the message there, and then catching the message in index.rb:

// save.rb
module Web::Controllers::Home
  class Save
    include Web::Action
    include Hanami::Action::Session

    def call(params)
      flash[:message] = 'Saved!'
      redirect_to routes.dashboard_path
    end
  end
end
// index.rb
module Web::Controllers::Home
  class Index
    include Web::Action
    include Hanami::Action::Session

    expose :message

    def call(params)
      p "HI THERE -> #{flash[:message]}"
      @message = flash[:message]
    end
  end
end

On the first load of index, it prints out "HI THERE -> Saved!"
On the second load, it prints out "HI THERE -> {:message=>\"Saved!\"}"

Am I doing something wrong? Is this related to #195?

@jodosha jodosha self-assigned this Dec 21, 2016
@jodosha jodosha added the bug label Dec 21, 2016
@jodosha
Copy link
Member

jodosha commented Dec 21, 2016

@greggilbert Hi Greg, I'm sorry about this problem.

Can you please try this for me?

module Web::Controllers::Home
  class Index
    include Web::Action
    include Hanami::Action::Session

    before :clear_flash # new callback

    expose :message

    def call(params)
      p "HI THERE -> #{flash[:message]}"
      @message = flash[:message]
    end

    private

    # new method
    def clear_flash
      flash.clear
    end
  end
end

Do these new lines of code solve your problem?

PS: I'm NOT advocating to keep these lines of code around forever, but just to try if this solution can work and eventually add it to the framework. 😉

@greggilbert
Copy link
Author

@jodosha, no need to apologize - always glad to help.

Yes, putting that change in place fixes it.

@jodosha jodosha added this to the v1.0.0.beta1 milestone Dec 21, 2016
@jodosha jodosha removed the waiting label Dec 21, 2016
@jodosha
Copy link
Member

jodosha commented Dec 21, 2016

@greggilbert Thanks for getting back. We'll write a patch for this. 👍

@matiasleidemer
Copy link

@greggilbert I'm trying to reproduce the error on my machine, but without success... Actually, the flash object is empty after the redirection, printing HI THERE -> on the console after it reaches the index action. Am I doing something wrong?

@greggilbert
Copy link
Author

@matiasleidemer Sorry about the delay. I just did a fresh install of Hanami and got the same thing as before. Did you enable sessions for the app?

I just put up a proof of concept. You should be able to just clone that, bundle install, and then run the server. Go to /, click the button, and you'll see the message. Reload that page (i.e. don't click the button again) and you'll see the second {:msg} statement.

@matiasleidemer
Copy link

@greggilbert cool, I'll take a look later today, thanks!

@matiasleidemer
Copy link

matiasleidemer commented Jan 8, 2017

Hello, what is the desired behavior here? I'd say the flash message shouldn't be available after the refresh, right? Since, according to the documentation It has a life span of one HTTP request or redirect. I'm asking just to make sure 😬

@greggilbert
Copy link
Author

@matiasleidemer Yeah, that's what I'd expect.

@matiasleidemer
Copy link

@greggilbert cool, I was able to fix it here, I will open a PR later today :)

@matiasleidemer
Copy link

#200 should take care of this, thanks for setting up a POC @greggilbert, it was really helpful... 😄

Since this is my first PR I'm not very sure I followed all the guidelines. Nevertheless, I'd be happy to address any other issue.

@greggilbert
Copy link
Author

Awesome. Thanks, all!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants