Rails - How can I set up an id as a token (pre-beta invitation application like usehipster)

373 views Asked by At

I am trying to write a "viral" pre-beta invitation application as one can see on usehipster.com or fork.ly.

Basically, the future tester:

1.) enters his email

2.) is redirected to a view (a coming_soon page)

3.) receive a link like this one : "http://localhost:3000/?referred_to=the tester's invitation id" displayed in the view.

4.) and receive an email with the same link.

If I understand it well, the "tester's invitation id" acts as a token in order to track from which testers the invitation come from.

My questions:

1.) How can I generate the id in the link? I cannot use before_create cause the invitation id is not already set up when a tester registered.

I tried this:

in the invitation controller

def coming_soon
  @invitation = Invitation.last
end

in views/invitations/coming_soon.html.erb

...
Copy and paste the following link to share wherever you want!</p>

<%= text_field_tag 'code', root_url + "?reffered_by=" + @invitation.id.to_s %>

Do you think they do like this?

2.) Why there is a question mark in the link? (or something like ?reffered_by= why not just root_url/@invitation.id.to_s) This is something related to the routes? is it a get method?

Thanks for your help!

1

There are 1 answers

0
rubyprince On BEST ANSWER

I will answer question 2:

? in url is a way to pass parameters. A form using GET method uses this sort of appending to url to pass parameters. URL ?reffered_by=somevalue passes the parameter referred_by with value somevalue. Now if in our controller, we want this value, we can call params[:referred_by] and we will get the value as "somevalue". If we want to pass more than one parameter, we can pass it using &. For example,

"#{root_url}?referred_by=#{@invitation.id}&referred_time_stamp=#{Time.now.strftime(%Y%m%d%H%M%S)}"

Now, we can access these parameters in controller as params[:referred_by] and params[:referred_time_stamp].

If you have no routes or resources defined in routes.rb, you cannot pass parameters like id using "#{root_url}/#{@invitation.id}". For example, when you define

map.connect ':controller/:action/:id'

in routes.rb, then it understands the part coming after last slash is the value of parameter id and we will get it as params[:id] in the controller.