0

On a Rails 4.x app I'm using Devise to register users. What I want to do is redirect a user after confirmation to the usual user_path(resource) but having a modal appear to prompt for a few things. I don't know how to solve this without using cookies and javascript. Anyone solved this before or know to do it using the standard after_confirmation_path_for way in Devise? If not, what do you think is the best approach to solve it?

Many thanks in advance.

Slenny
  • 379
  • 2
  • 4
  • 13
  • out of the hat idea, pass additional param in your path helper, if param.present? -> show modal – mmln Jan 18 '15 at 18:00
  • thanks @mymlyn , yeah, that's what I'll do with javascript if there's no Railsy idiomatic way of doing this. – Slenny Jan 19 '15 at 11:18

1 Answers1

1

Ok, so if anyone else runs into this problem. I only found one way to solve it, this is it:

confirmations_controller.rb

class ConfirmationsController < Devise::ConfirmationsController

  private

  def after_confirmation_path_for(resource_name, resource)
    sign_in(resource)
    user_path(resource[:id], cp: true)
  end
end

Then in the view which I want to popup the modal, I used this fairly popular javascript answer (How can I get query string values in JavaScript?) show.html.erb:

<%= javascript_tag do -%>
window.onload = function() {
    var doCP = getParameterByName('cp');
    if (doCP) {
        $("#modal").html("<%= escape_javascript(render 'confirm_prompt_form') %>");
        $("#modal").modal("show");
    }
}

function getParameterByName(name) {
    name = name.replace(/[\[]/, "\\[").replace(/[\]]/, "\\]");
    var regex = new RegExp("[\\?&]" + name + "=([^&#]*)"),
        results = regex.exec(location.search);
    return results === null ? "" : decodeURIComponent(results[1].replace(/\+/g, " "));
}
Community
  • 1
  • 1
Slenny
  • 379
  • 2
  • 4
  • 13