0

Hello i know this is common problem but i cant let it work.. I need account activation from email link.. url looks like http://domain.tld/activation/[email protected]/d4sa89dsa98 and i want access that parameters from controller :-)

routes.rb

match "activation/:email/:activation_key" => "frontend#activation", :via => :get

frontend_controller.rb

  def activation
    @user = User.find(:conditions => {:email => params[:email], activation_key => params[:activation_key]})
    render "activation"
  end

and im getting error

No route matches [GET] "/activation/[email protected]/a46d4sa8dsa68d"

my activation.html.erb

<% if @user %>
Activation successful.
<% else %>
Activation key is invalid.
<% end %>

EDIT: ---------------------------------------------------------------------------------------------------------------------------

I replaced :email parameter with :id parameter because it look nicer, im not sure if Array.new(32){Random.new.rand(36).to_s(36)}.join is unique string but that does not matter now.

route

match "activation/:id/:activation_key" => "frontend#activation", :via => :get

rake routes

GET    /activation/:id/:activation_key(.:format) frontend#activation

frontend controller

  def activation
    @user = User.find(:conditions => {:id => params[:id], :activation_key => params[:activation_key]})
    render "activation"
  end

error

Couldn't find User without an ID

but this WORKS so the problem is in the condition

@user = User.find(params[:id])

SOLUTION

@user = User.find(:first, :conditions => {:id => params[:id], :activation_key => params[:activation_key]})
2
  • 1
    Why do you have the email in there? I have never seen that before. I would only use the activation key if I were you. Commented Aug 19, 2013 at 3:37
  • you are absolutely right, i wasnt sure about uniqueness of that generator so i add email but id of user will be better or i can use SecureRandom.uuid.. i changed :email to :id but now it says Couldn't find User without an ID that means params[:id] doesnt work.. i will edit question for more information Commented Aug 19, 2013 at 9:51

1 Answer 1

3

Have you tried adding a constraints: { email: /[^\/]+/ } to your match argument? Possibly the . gets eaten up by overly greedy regex.

Sign up to request clarification or add additional context in comments.

1 Comment

Hello thanks, i replaced email for id.. please look edited question

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.