Search code examples
ruby-on-railsroutesregistrationactivation-codes

Spaghetti Rails Routing and Invitation Coding Bolognese


I'm not really sure if there is a single problem here or I have approached this in the wrong way, but any suggestions would be greatly appreciated!

In the application when a user signs up they get privileges and their page gets set up, they can then invite colleagues by email.

The email has an activation code tagged on the end. An example url would be "/user/new/xxxxxxxxx".

Here's the problem: I need the colleague to be able to create a User account as well, with just their basic info, so they can log into the account and set up their corner of the app.

When the colleague makes a mistake on the user sign up form, the url forgets there is an activation code, and jumps back with validation messages and a pretty bare url of '/users'. When the colleague amends their mistakes and clicks sign up, they are submitted as a fully fledged user, not an invited colleague.

This happens because i have an if clause on the 'users/new' page

<% if @activation_code %>
  Show colleague messages of invitation and happiness
<% else %>
  Show fully fledged user ego stroking messages
<% end %>

My routing to find the code parameter of the url is:

map.signup '/users/new/:code', :controller => 'users', :action => 'new', :code => nil

Like I said before, have I approached this completely wrong? is is there one problem here?

UPDATE This Rails Cast Episode Solved nearly all of the problems I was having: Beta Invitations

Although to distinguish if the person was coming from an invitation or not I just used this conditions block:

if [email protected]_id.blank?

and that worked great.


Solution

  • Im guessing your controller looks like this:

    def create
      if @user = User.create(params[:user]) && @user.new_record?
        #take the user to where you want them to go
      else
        #there was an error
        flash[:error] = "Oops, blah blah blah"
        render :action => "new"
      end
    end
    

    Problem is that you dont have the @activation_code in the view anymore. So, I would suggest that you pass the activation_code back in a hidden form field.

    def create
      @activation_code = params[:activation_code]
      if @user = User.create(params[:user]) && @user.new_record?
        #take the user to where you want them to go
      else
        #there was an error
        flash[:error] = "Oops, blah blah blah"
        render :action => "new"
      end
    end
    

    This way when you render the "new" view from the create action your view will still have the required @activation_code to help it render the appropriate conditional elements.