Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Keeping a query-string in devise sign up

I am using rails with devise for signing up. Also I added an invite code, so not everybody can sign up.

The invite code gets transmitted via query-string like "/users/sign_up?invite_code=wajdpapojapsd" and gets added to a hidden field of the sign-up form with "f.hidden_field :invite_code, :value => params[:invite_code]".

This works pretty well. The only problem is that if the sign up doesn't get validated and rejected, devise redirects to "/users" and loses the query string with the invite_code in it.

As the email stays in the sign up form after the failed attempt, I believe that this should also work for the invite code. As a worst case solution redirecting :back after a failed sign up and losing the email, but keeping the invite code, would be better than the way it works now.

EDIT: By now I ve set up a registration controller for devise, but don't have any idea how to get the desired behavior.

Any help on how to keep the query string or just the invite code would be awesome.

like image 391
James Lever Avatar asked Aug 11 '12 11:08

James Lever


2 Answers

I found a working solution by now.

I used jstr's answer to set up the controller for devise and added the "session" line.

class MyRegistrationsController < Devise::RegistrationsController
  prepend_view_path "app/views/devise"

  def create
    super
    session[:invite_code] = resource.invite_code
  end

  def update
    super
  end
end

Afterwards I added following to the devise/registrations/new.html.erb

<% if params[:invite_code]
  @invite_code = params[:invite_code]
else
  @invite_code = session[:invite_code]
end %>

And changed the hidden_field to

<%= f.hidden_field :invite_code, :value => @invite_code %>
like image 136
James Lever Avatar answered Sep 26 '22 04:09

James Lever


You might need to make your own subclassed Devise controllers to get this to work.

This answer has a good description of how to to this.

The basics:

  1. Install the Devise views if you haven't already with rails generate devise:views
  2. Create a subclassed Devise::RegistrationsController
  3. Update your Devise routes declaration to get Devise to use your subclassed controller
like image 26
jstr Avatar answered Sep 25 '22 04:09

jstr