Maintaining a query string in development

I use rails with a device to register. I also added an invitation code, so not everyone can register.

The invitation code is transmitted through a query string like "/ users / sign_up? Invite_code = wajdpapojapsd" and added to the hidden field of the registration form using "f.hidden_field: invite_code ,: value => params [:. Invite_code]"

It works very well. The only problem is that if the registration is not checked and not rejected, create redirects to "/ users" and lose the query string with the invitation in it.

Since the email remains in the registration form after an unsuccessful attempt, I believe that this should also work for the invitation code. As the worst solution for solving problems: back after unsuccessful registration and loss of email, but keeping the invitation code will be better than now.

EDIT: So far, I have created a registration controller for development, but I don’t know how to achieve the desired behavior.

Any help on how to save the query string or just the invitation code would be amazing.

+4
source share
4 answers

I found a working solution.

I used jstr to configure the controller for development and added a session string.

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 

Subsequently, I added the following to devise / registration / new.html.erb

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

And changed hidden_field to

 <%= f.hidden_field :invite_code, :value => @invite_code %> 
+6
source

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

This answer contains a good description of how to do this.

The basics:

  • Install Devise views if you don't already have rails generate devise:views
  • Subclass Devise::RegistrationsController
  • Update your Devise Routes declaration to force Devise to use your subclass controller.
+1
source

why not use an instance variable like this

 @invite_code = params[:invite_code] 

Then, when the registration fails, you can use this variable in the view that will be displayed for failed registration.

I mean before, it is redirected, you have to save the invitation code parameters with the instance variable. Sorry if I'm wrong.

0
source

The @James Lever solution has one drawback. invite_code remains in the session. In some solutions, this can cause problems. An alternative soul will be:

 def create # set invite code to session: session[:invite_code] = resource.invite_code # here the form is rendered and invite code from session would be used: super # delete invite code from session: session.delete('invite_code') end 
0
source

All Articles