Ruby on Rails将输入与数据库进行比较

时间:2014-05-27 09:13:12

标签: ruby-on-rails ruby

我们正在建立一个登录系统,您必须使用激活码激活您的帐户。每个用户都有一个唯一的代码。 我们无法弄清楚如何制作一个表单来检查该表单的输入是否等于唯一的激活码。

表格:

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

控制器动作

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

用户表

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

路线档案

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'

1 个答案:

答案 0 :(得分:1)

@user定义在哪里?,请改为

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