从设计中覆盖注册控制器时,是否可以在创建操作上访问新创建的用户?(When overriding the registration controller from devise, is it poss

编程入门 行业动态 更新时间:2024-10-28 15:21:57
从设计中覆盖注册控制器时,是否可以在创建操作上访问新创建的用户?(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 end

routes.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 end

Notes:

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.

更多推荐

本文发布于:2023-07-20 12:50:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1199690.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:控制器   操作   用户   registration   controller

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!