views:

38

answers:

2

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.

A: 

Once they've visited the page using the confirmation code, you might considering including the confirmation code in a <input type="hidden" /> tag to insure that it's saved between postbacks.

Otherwise, you need to modify the action for your sign-up form to include the activation code; something like this:

form_for @user, :url => "/users/new/#{@activation_code}" do |f|
# ...
end
meagar
I tried adding in the url like you said, but i when submit is clicked it sends it to 'users/new/users/new/xxx' - I can't seem to find a way around that. I would like to use the url option against the url but the hidden field is a good point!
Joe
Have you got a leading forward slash on your `:url` option?
meagar
Yes I did. I ended up using the railscast I mentioned in my update and used a hidden field, like you said, and virtual attributes to solve the problem. thanks for you input!
Joe
+1  A: 

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.

Apie
You were right that I needed a hidden field, and without a virtual attribute I would of had to use your code. Thanks for your time.
Joe
Glad to help :)
Apie