如果用户创建,Rails会创建记录

时间:2016-05-17 17:51:42

标签: mysql ruby-on-rails

如果新用户注册,则自动为他创建job_app状态。整个数据库都在mysql上,我与User model中的问题没有关联(只有验证和has_one)问题是job_apps中user_id = current_user.user_id的新记录没有创建(真诚的我不知道该怎么做。)

class UsersController < ApplicationController
    include job_apps_helper
    def new
        @users = User.new
    end

    def create
        @user = User.new(user_params)
        if @user.save 
            session[:user_id] = @user.id 
            current_user = @user.id

            redirect_to '/status' 
        else 
            redirect_to '/signup' 
        end 
    end
    private
        def user_params
        params.require(:user).permit(:first_name, :last_name, :email, :PESEL, :phone, :cv, :password)
        end
end


class JobAppsController < ApplicationController
    before_action :require_user, only: [:index]
    before_action :require_emp, only: [:empl]
    #def new 
    #   @job_app = job_app.new
    #end

    def index 
        @users = User.includes(:job_apps)
        end
        def empl
            @users = User.all.where(:user_type => 'candidate')
        end

    end
class JobApp < ActiveRecord::Base
    belongs_to :user dependent: :create, :destroy
    enum status: [ :waiting, :accepted , :refused  ]
    def accept 
        self.update(status: 1)
    end
    def refuse
        self.update(status: 2)
    end
    end
end

我的路线:

root 'stat_page#home'
    get 'status' => 'job_apps#index'
    get 'stat_emp' => 'job_apps#empl'
    get 'login' => 'sessions#new'
    post 'login' => 'sessions#create'
    delete 'logout' => 'sessions#destroy'
    get 'logout' => 'sessions#destroy'
    get 'signup' => 'users#new'
    resources :users

1 个答案:

答案 0 :(得分:0)

您应该在用户模型上使用callback

def distance(v1,v2): 
    return sum([(x-y)**2 for (x,y) in zip(v1,v2)])**(0.5)