在 Rails 中自定义 Devise 视图
我正在使用 devise 进行用户身份验证,但我有很好的注册、登录等页面的模型。 我已经完成了 railsgenerate devise:views User
命令并在views文件夹中拥有了所有视图,但是,当我用我自己的new.html替换registration/new.html.erb时.erb,没有任何变化,也没有看起来不同。就好像我做了什么一样。
任何人都知道我做错了什么,或者至少知道如何成功定制设计视图
。需要注意的是我将 devise/registration#new 的路径更改为 /signup 吗?
I'm using devise for user auth, but I have nice mockups for the signup, login, etc. pages.
I've already done the rails generate devise:views User
command and have all of the views in the views folder, however, when I replaced the registration/new.html.erb with my own new.html.erb, nothing changes nor looks different. It's as if I had done anything.
Anyone know what I'm doing wrong or at least how to successfully customize devise views
P.S. Is it important to note that I changed the route of devise/registration#new to /signup?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
答案一目了然。
...而不是
使用:
如果您已经这样做了,请将从
app/views/User
创建的文件夹 devise 移动到新文件夹app/views/devise
(或者只需将User
文件夹重命名为devise
(如果可以的话)。)这些文件夹是:
无需进行其他更改。
at a glance answer.
...instead of
use:
If you've already done it, move the folders devise created from
app/views/User
to a new folderapp/views/devise
(or just rename theUser
folder todevise
, if that's an option.)Those folders are:
No other changes are necessary.
虽然这是一个老问题,但我想我会补充一下,以防有人偶然发现它。我不确定这是否是一个新的补充,因为这个问题最初是被问到的,但如果是这样,更简单(更现代)的方法就是这样。
在文件
config/initializers/devise.rb
中有以下代码块:取消注释
config.scoped_views = false
并将其值更改为true,devise 会自动检查自定义视图是否存在,如果存在,则提供该视图。
正如评论所说,它可能会增加应用程序的一些开销,但根据我迄今为止的经验,这是可以忽略不计的。
though this is an old question, I thought I'd add to it in case anybody stumbles on it. I'm not sure if this is a new addition since the question was originally asked but if so the simpler (more modern) approach is this.
in the file
config/initializers/devise.rb
there is the following block of code:by uncommenting
config.scoped_views = false
and changing it's value totrue
, devise will automatically check whether the custom view exists and if so, serve that up.As the comment says, it may add some overhead to the application but in my experience so far, this is negligible.
您的路线
signup
或devise/registrations#new
将呈现视图views/devise/registrations/new.html.erb
。听起来像是你做的更改
views/user/registrations/new.html.erb
,这可以解释为什么您看不到所做的更改,因为它没有被渲染。
您需要创建一个
user/registrations_controller.rb
从
Devise::RegistrationsController
扩展并指向您的/signup
路由至
user/registrations#new
,或者您也可以直接进行更改直接访问
views/devise/registrations/new.html.erb
同样的想法也适用于您的登录 (
devise/sessions
) 页面。希望这有帮助。
Your route
signup
ordevise/registrations#new
will render the viewviews/devise/registrations/new.html.erb
. It sounds like you madechanges to
views/user/registrations/new.html.erb
, which would explainwhy you dont see the changes made since its not being rendered.
You will either need to create a
user/registrations_controller.rb
thatextends from
Devise::RegistrationsController
and point your/signup
route to
user/registrations#new
, or you can just make your changesdirectly to
views/devise/registrations/new.html.erb
Same idea applies to your login (
devise/sessions
) pages.Hope this helps.
对于仍然遇到此问题的任何人,问题在于对
railsgenerate devise:views User
的调用。它应该是railsgenerate devise:views
用于从 Devise Rails Engine 获取当前视图。这将生成适用于默认路由的正确视图。For anyone still having a problem with this, the problem lies in the call to
rails generate devise:views User
. It should berails generate devise:views
for fetching current views from the Devise Rails Engine. This will generate proper views which will work with the default routes.生成自定义视图后,例如
在
config/initializer/devise.rb
中打开scoped_views
就完成了。
After generating your custom views e.g
Turn on
scoped_views
inconfig/initializer/devise.rb
And you are done.
使用
rails g devise:views User
允许您在拥有多个角色时进行自定义。正确的方法是进入 config/initializer/ 文件夹中的 devise.rb
并取消注释并设置 config.scoped_views = true 。
现在您可以毫无问题地编辑查看 erb 文件
Using
rails g devise:views User
allows you to customize when you have more than one role.the proper way to do this is going into your
devise.rb
inconfig/initializer/
folderand uncommenting and setting
config.scoped_views = true
.now you can edit the view erb files without any problems
我遇到了同样的问题,直到我回去阅读设计文档:)
在
railsgenerate devise:views
之后,确保进入initializers/devise.rb
并设置>config.scoped_views = true
。 https://github.com/plataformatec/devise 的设计文档以及devise.rb
注释。执行此操作后,
views/users
中我自己的视图开始显示,而不是 gem 中的视图。I had the same problem until I went back and read the devise documentation :)
After
rails generate devise:views
make sure you go intoinitializers/devise.rb
and setconfig.scoped_views = true
. This is explained in the devise documentation at https://github.com/plataformatec/devise as well as in thedevise.rb
comments.After I did this, my own views in
views/users
started showing up instead of the ones in the gem.为了将来参考,您只需从 devise => 重命名文件夹即可用户,反之亦然,rails 会找到一条路线。
For future reference, you can just rename folder from devise => user and vice versa and rails will find a route.