在 Rails 中自定义 Devise 视图

发布于 2024-11-19 18:31:38 字数 297 浏览 2 评论 0原文

我正在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

埋葬我深情 2024-11-26 18:31:38

答案一目了然。

...而不是

rails generate devise:views User

使用:

rails generate devise:views

如果您已经这样做了,请将从 app/views/User 创建的文件夹 devise 移动到新文件夹 app/views/devise (或者只需将 User 文件夹重命名为 devise(如果可以的话)。)

这些文件夹是:

app/views/User/confirmations
app/views/User/mailer
app/views/User/passwords
app/views/User/registrations
app/views/User/sessions
app/views/User/shared
app/views/User/unlocks

无需进行其他更改。

at a glance answer.

...instead of

rails generate devise:views User

use:

rails generate devise:views

If you've already done it, move the folders devise created from app/views/User to a new folder app/views/devise (or just rename the User folder to devise, if that's an option.)

Those folders are:

app/views/User/confirmations
app/views/User/mailer
app/views/User/passwords
app/views/User/registrations
app/views/User/sessions
app/views/User/shared
app/views/User/unlocks

No other changes are necessary.

書生途 2024-11-26 18:31:38

虽然这是一个老问题,但我想我会补充一下,以防有人偶然发现它。我不确定这是否是一个新的补充,因为这个问题最初是被问到的,但如果是这样,更简单(更现代)的方法就是这样。

在文件 config/initializers/devise.rb 中有以下代码块:

# ==> Scopes configuration
# Turn scoped views on. Before rendering "sessions/new", it will first check for
# "users/sessions/new". It's turned off by default because it's slower if you
# are using only default views.
# config.scoped_views = false

取消注释 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:

# ==> Scopes configuration
# Turn scoped views on. Before rendering "sessions/new", it will first check for
# "users/sessions/new". It's turned off by default because it's slower if you
# are using only default views.
# config.scoped_views = false

by uncommenting config.scoped_views = false and changing it's value to true, 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.

满身野味 2024-11-26 18:31:38

您的路线 signupdevise/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 or devise/registrations#new will render the view
views/devise/registrations/new.html.erb. It sounds like you made
changes to views/user/registrations/new.html.erb, which would explain
why you dont see the changes made since its not being rendered.

You will either need to create a user/registrations_controller.rb that
extends from Devise::RegistrationsController and point your /signup
route to user/registrations#new, or you can just make your changes
directly to views/devise/registrations/new.html.erb

Same idea applies to your login (devise/sessions) pages.

Hope this helps.

以为你会在 2024-11-26 18:31:38

对于仍然遇到此问题的任何人,问题在于对 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 be rails generate devise:views for fetching current views from the Devise Rails Engine. This will generate proper views which will work with the default routes.

原来分手还会想你 2024-11-26 18:31:38

生成自定义视图后,例如

rails generate devise:views User

config/initializer/devise.rb 中打开 scoped_views

view config.scoped_views = true

就完成了。

After generating your custom views e.g

rails generate devise:views User

Turn on scoped_views in config/initializer/devise.rb

view config.scoped_views = true

And you are done.

﹏雨一样淡蓝的深情 2024-11-26 18:31:38

使用 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 in config/initializer/ folder

and uncommenting and setting config.scoped_views = true.

now you can edit the view erb files without any problems

彩扇题诗 2024-11-26 18:31:38

我遇到了同样的问题,直到我回去阅读设计文档:)

railsgenerate devise:views之后,确保进入initializers/devise.rb并设置>config.scoped_views = truehttps://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 into initializers/devise.rb and set config.scoped_views = true. This is explained in the devise documentation at https://github.com/plataformatec/devise as well as in the devise.rb comments.

After I did this, my own views in views/users started showing up instead of the ones in the gem.

回忆凄美了谁 2024-11-26 18:31:38

为了将来参考,您只需从 devise => 重命名文件夹即可用户,反之亦然,rails 会找到一条路线。

For future reference, you can just rename folder from devise => user and vice versa and rails will find a route.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文