我创建了一个userstamps方法来了解是哪个用户创建和编辑了数据。我有一个表Illustrator,我正试图将created_by分配给current_user id。但当它保存时,created_by为零,并且没有显示任何错误。当我把raise放在illustrator.save之前时,@illustrator.created_by正是current_user.id,但当我检查db中的值时,它为零。
class User < ApplicationRecord
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :validatable,
:custom_authenticatable
belongs_to :account
belongs_to :role
cattr_accessor :current_user
validates :email, presence: true, uniqueness: true
validates :email, format: { with: URI::MailTo::EMAIL_REGEXP }
validates :password,
length: { minimum: 6 },
if: -> { new_record? || !password.nil? }
enum gen: { masculino: 1, feminino: 2 }
private
#apenas usuário com role de admim pode logar.
def valid_for_custom_authentication?(password)
#self.role.name == 'admin'
self.role_id === 2
end
end
class ApplicationController < ActionController::Base
before_action :authenticate_user!
helper_method :current_user
private
def current_user=(user)
@current_user = user
end
end
class IllustratorsController < ApplicationController
before_action :authenticate_user!
def new
@illustrator = Illustrator.new
end
def create
@illustrator = Illustrator.new(illustrator_params)
@illustrator.created_by = current_user.id
if @illustrator.save
redirect_to illustrator_path(@illustrator), notice: 'O autor foi criado com sucesso.'
else
render :new
end
end
private
def illustrator_params
params.require(:illustrator).permit(:name, :display_name, :email, :phone, :status, :notes, :created_by)
end
end
Rails.application.routes.draw do
get 'payments/index'
devise_for :users
root to: 'pages#home'
resources :books
resources :authors
resources :publishers
resources :illustrators
resources :plans do
resources :accounts, only: %i[new create] do
end
end
resources :payments, only: %i[index]
resources :accounts, only: %i[index show edit update destroy] do
resources :users, only: %i[new create] do
resources :roles
end
end
resources :users, only: %i[edit update index show destroy]
#API routes
namespace :api do
namespace :v1 do
get 'default/:id', to: 'plans#change_plans'
get 'plans', to: 'plans#index'
get 'plans/id', to: 'plans#show'
get 'accounts', to: 'accounts#return'
post 'users', to: 'users#create'
#resources :users
post '/auth/login', to: 'authentication#login'
get '/*a', to: 'application#not_found'
post 'password/forgot', to: 'users#forgot_password'
end
end
end
如果你正在使用设备,你不需要这个部分:
helper_method :current_user
private
def current_user=(user)
@current_user = user
end
你在哪里找到的?从你的代码库中删除它!
使用devise
,您不需要任何getter或setter方法。
这可能是它对你不起作用的原因。
如果在application_controller
中您有before_action :authenticate_user!
,current_user
方法应该可以开箱即用!
同样,你不需要在这里再次添加before_action :authenticate_user!
class IllustratorsController < ApplicationController
before_action :authenticate_user!
,因为您已经在application_controller中将其设置为应用程序范围的。
你的create
方法很好。试着去除多余的东西,一切都会好起来的。
您的控制器中只有setter方法,用于设置current_user
def current_user=(user)
@current_user = user
end
您需要添加getter方法以及以下方法,以便您可以在控制器中使用它
def current_user
@current_user
end
在ruby中,有这个attr_accessor :current_user
的捷径参考这个