0

We are making a Login system where you must activate your account with an activation code. Every user has a unique code. We can't figure out how to make a form that checks that the input of that form is equal to the unique activation code.

The form:

<%= form_for :user, :controller => :users, :action => 'check_authentication_token' do |f| %>
<%= f.text_field :authentication_token, :value => '' %>
<%= f.submit 'Activeren' %>
<% end %>

The controller action

def check_authentication_token
  user = User.find_by(authentication_token: params[:user][:authentication_token])
  if user && user.authenticate(user_params[:authentication_token])
    redirect_to @user
  else
    redirect_to competitions_path
  end
end

User table

create_table "users", force: true do |t|
  t.string   "name"
  t.string   "email"
  t.datetime "created_at"
  t.datetime "updated_at"
  t.string   "password_digest"
  t.string   "remember_token"
  t.boolean  "activated",            default: false
  t.string   "authentication_token"
end

Route file

Wisemonkeys::Application.routes.draw do
get "users/new"
resources :competitions
resources :users 
get 'pictures/new'
get 'competitions/new'
resources :pictures do
  member do
get 'upvote'
end
end
get '/mypictures' => 'pictures#mypictures'
get '/voteresults' => 'pictures#voteresults'
get '/activatie' => 'users#check_authentication_token'
root 'competitions#index'
resources :sessions, only: [:new, :create, :destroy]
match '/signup',  to: 'users#new',            via: 'get'
match '/signin',  to: 'sessions#new',         via: 'get'
match '/signout', to: 'sessions#destroy',     via: 'delete'
2
  • 1
    @user is undefined. Either change that to user or change user = User.find_by ... to @user = User.find_by ... Commented May 27, 2014 at 9:17
  • Show your route as well. Is ther any issue with user_params[:authentication_token] and params[:user][:authentication_token] . Please confirm Commented May 27, 2014 at 9:21

1 Answer 1

1

Where's @user defined ?, do this way instead

def check_authentication_token
  user = User.find_by(authentication_token: params[:user][:authentication_token])
  if user.present?
    redirect_to user
  else
    redirect_to competitions_path
  end
end
Sign up to request clarification or add additional context in comments.

Comments

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.