Ruby-on-rails 在 Rails 中自定义设计视图
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6646845/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Customizing Devise views in Rails
提问by Vasseurth
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 Usercommand 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.
我正在使用设计进行用户身份验证,但我有很好的注册、登录等页面模型。我已经完成了rails generate devise:views User命令并且在views 文件夹中有所有的视图,但是,当我用我自己的new.html.erb 替换registration/new.html.erb 时,没有任何变化,看起来也没有什么不同。好像我做了什么。
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?
PS 重要的是要注意我将设计/注册#new 的路线更改为/signup?
采纳答案by Kevin Tsoi
Your route signupor devise/registrations#newwill 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.
您的路线signup或devise/registrations#new将呈现视图
views/devise/registrations/new.html.erb。听起来您对 进行了更改views/user/registrations/new.html.erb,这可以解释为什么您没有看到所做的更改,因为它没有被渲染。
You will either need to create a user/registrations_controller.rbthat
extends from Devise::RegistrationsControllerand point your /signuproute to user/registrations#new, or you can just make your changes
directly to views/devise/registrations/new.html.erb
您将需要创建一个user/registrations_controller.rb从Devise::RegistrationsController您的/signup路线延伸并指向您的路线user/registrations#new,或者您可以直接对views/devise/registrations/new.html.erb
Same idea applies to your login (devise/sessions) pages.
同样的想法也适用于您的登录 ( devise/sessions) 页面。
Hope this helps.
希望这可以帮助。
回答by ocodo
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/Userto a new folder app/views/devise(or just rename the Userfolder to devise, if that's an option.)
如果您已经这样做了,请将创建的文件夹移动app/views/User到一个新文件夹app/views/devise(或者只是将User文件夹重命名为devise,如果这是一个选项。)
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.
无需其他更改。
回答by DazBaldwin
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.rbthere is the following block of code:
在文件中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
by uncommenting config.scoped_views = falseand changing it's value to true, devise will automatically check whether the custom view exists and if so, serve that up. As it says it does add some overhead to the application but in my experience so far this is minimal.
通过取消注释config.scoped_views = false并将其值更改为true,设计将自动检查自定义视图是否存在,如果存在,则提供该视图。正如它所说,它确实给应用程序增加了一些开销,但根据我的经验,到目前为止这是最小的。
回答by Marcus W
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:viewsfor fetching current views from the Devise Rails Engine. This will generate proper views which will work with the default routes.
对于仍然有此问题的任何人,问题在于调用rails generate devise:views User. 它应该rails generate devise:views用于从设计 Rails 引擎获取当前视图。这将生成适用于默认路由的正确视图。
回答by Saqib R.
After generating your custom views e.g
生成自定义视图后,例如
rails generate devise:views User
Turn on scoped_viewsin config/initializer/devise.rb
开启scoped_views中config/initializer/devise.rb
view config.scoped_views = true
And you are done.
你已经完成了。
回答by guero64
I had the same problem until I went back and read the devise documentation :)
在我回去阅读设计文档之前,我遇到了同样的问题:)
After rails generate devise:viewsmake sure you go into initializers/devise.rband set config.scoped_views = true. This is explained in the devise documentation at https://github.com/plataformatec/deviseas well as in the devise.rbcomments.
在rails generate devise:views确保你进入initializers/devise.rb并设置之后config.scoped_views = true。这在https://github.com/plataformatec/devise的设计文档以及devise.rb评论中进行了解释。
After I did this, my own views in views/usersstarted showing up instead of the ones in the gem.
在我这样做之后,我自己的观点views/users开始出现,而不是宝石中的观点。
回答by Richard Lau
Using rails g devise:views Userallows you to customize when you have more than one role.
使用rails g devise:views User允许您在拥有多个角色时进行自定义。
the proper way to do this is going into your devise.rbin config/initializer/folder
正确的方法是进入你devise.rb的config/initializer/文件夹
and uncommenting and setting config.scoped_views = true.
并取消注释和设置config.scoped_views = true。
now you can edit the view erb files without any problems
现在您可以毫无问题地编辑查看 erb 文件
回答by Richard Lau
For future reference, you can just rename folder from devise => user and vice versa and rails will find a route.
为了将来参考,您可以从 devise => user 重命名文件夹,反之亦然,rails 将找到一条路线。

