0

I have the following resources

Places (:place_id, :name, :shortlink, :token)

Events (:event_id, :address)

I have a restricted user registration with a nested user resource (User has many Events).

When User register, (s)he create a new event with a referenced place column.

Users can only sign up when having theses parameters in the url

https://mysite/user/signup?q=[:place_id]&t=[:token] # place id must exist, place token must be equal to t parameter

before_action :check_has_access, only: [:new]
...
protected

def check_has_access
  @place = Place.find(params[:q])
   if params[:q].blank? && @place.blank? || params[:t].blank? || @place.token != params[:t]
     redirect_to(root_path)
     flash[:alert] = 'Restricted sign-up'
   end
end

I want to generate a shortcut for each place for the user to register

https://mysite/[:shortlink] #place.shortlink

That will redirect to a given sign up form with corresponding filled form and restricted params so the user that has the direct url can sign up.

https://mysite/user/signup?q=[:place_id]&t=[:token]

How can I have routes to be generated ?

Subsidiary question, am I doing it right ?

petitkriket
  • 154
  • 4
  • 19

1 Answers1

1

Found a solution on this post here.

I need to restart the server so newly created route get loaded after a Place object creation. Not sure if I am doing it right though but it works..

#config/route.rb

 Rails.application.routes.draw do
    Place.all.each do |pl|
         get "/#{pl.shortlink}" => redirect("/users/sign_up?q=#{pl.id}&t=#{pl.token}")
       end
    end

In order to load the newly created route when adding a Place, I added

#models/place.rb

after_create :add_shortlink
...
  def add_shortlink
  Rails.application.reload_routes!
end

This will not work on Heroku, issue addressed is here

petitkriket
  • 154
  • 4
  • 19