从设计中覆盖注册控制器时,是否可以在创建操作上访问新创建的用户?(When overriding the registration controller from devise, is it possible to access the newly created user on the create action?)
我正在尝试在用户注册后立即创建一个文件夹,所以我覆盖了注册控制器上的创建操作(设计),但我不知道如何访问新创建的用户以创建具有其名称的文件夹稍后上传文件。
到目前为止,我有这个:
class RegistrationsController < Devise::RegistrationsController def new super end def create super create_folder end def update super end def create_folder path = Pathname.new(':rails_root/tmp/') directory_name = ":current_user" Dir.mkdir(path, directory_name) unless File.exists?(directory_name) end end的routes.rb
devise_for :users, :controllers => {:registrations => "registrations"}我按照这个来覆盖注册控制器。
我应该把它留在那里还是将它移到创建动作中? 而不是使用方法 是访问当前用户的正确方法吗? 也许不是注册而是在登录时做得更好?我很感激我能得到的任何帮助。
I'm trying to create a folder right after a user registers, so I override the create action on the registration controller (devise) but I don't know how to access the newly created user in order to create the folder with it's name to upload files later.
So far I've got this:
class RegistrationsController < Devise::RegistrationsController def new super end def create super create_folder end def update super end def create_folder path = Pathname.new(':rails_root/tmp/') directory_name = ":current_user" Dir.mkdir(path, directory_name) unless File.exists?(directory_name) end endroutes.rb
devise_for :users, :controllers => {:registrations => "registrations"}I followed this to override the registration controller.
Should I leave it there or move it to the create action? Instead of using a method is that the right way to access the current user? Maybe instead of registration it's better to do it on sign in?I'd appreciate any help I can get.
最满意答案
您应该在after_filter为您的操作完成此功能,而不是在操作本身中:
class RegistrationsController < Devise::RegistrationsController after_filter :create_folder, :only => :create protected def create_folder path = Pathname.new(Rails.root.to_s + '/tmp/') #=> Note 1 directory_name = resource.id.to_s #=> Note 2 Dir.mkdir(path + directory_name) #=> Note 3 end end备注 :
检索项目根目录的语法不正确。 :rails_root无法在单引号内插入 - 并且它不存在,无论如何。 请尝试使用Rails.root.to_s 。 ":current_user"只是一个字符串,它将尝试命名目录:current_user ,它既不是动态的也不是有效的。 因为您的Devise控制器可以访问当前resource (在本例中,代表current_user ),所以检索resource.id并使用它。 将path和directory_name与+连接在一起,而不是。 unless有条件, unless不需要,因为您可能只在创建新用户时创建新文件夹,并且每个用户都有唯一的id 。You should really be accomplishing this functionality in an after_filter to your action, rather than in the action itself:
class RegistrationsController < Devise::RegistrationsController after_filter :create_folder, :only => :create protected def create_folder path = Pathname.new(Rails.root.to_s + '/tmp/') #=> Note 1 directory_name = resource.id.to_s #=> Note 2 Dir.mkdir(path + directory_name) #=> Note 3 end endNotes:
Your syntax for retrieving the project root is incorrect. :rails_root can't be interpolated from within single quotes - and it doesn't exist, anyways. Try Rails.root.to_s instead. ":current_user" is simply a string that will attempt to name the directory :current_user, which is neither dynamic nor valid. Because your Devise controller has access the the current resource (which in this case, represents the current_user), retrieve the resource.id and use it instead. Concatenate the path and directory_name together with +, not ,. There's no need for the unless conditional, since you're presumably only creating the new folder upon creation of a new user, and each user has a unique id.更多推荐
发布评论