如何定制devisedevise 可以帮助你快速构建应用程序,同时也支持定制化。实际上 devise 是一个引擎,它的所有 views 和 controllers 都被打包在 gem 中,你只需要用 generate 命令,这些文件就会自动拷贝到你的项目中。你只需要重写这些文件,就可以实现 devise 的定制化。
另外需要配置 config/initializers/devise.rb。
$ rails generate devise:views users
$ rails generate devise:controllers users
清单8. generate生成devise文件1
| shirleydembpeveloperWork shirley$ rails generate devise:views users
|
Running via Spring preloader in process 61411
invoke Devise::Generators::SharedViewsGenerator
create app/views/users/shared
create app/views/users/shared/_links.html.erb
invoke form_for
create app/views/users/confirmations
create app/views/users/confirmations/new.html.erb
create app/views/users/passwords
create app/views/users/passwords/edit.html.erb
create app/views/users/passwords/new.html.erb
create app/views/users/registrations
create app/views/users/registrations/edit.html.erb
create app/views/users/registrations/new.html.erb
create app/views/users/sessions
create app/views/users/sessions/new.html.erb
create app/views/users/unlocks
create app/views/users/unlocks/new.html.erb
invoke erb
create app/views/users/mailer
create app/views/users/mailer/confirmation_instructions.html.erb
create app/views/users/mailer/email_changed.html.erb
create app/views/users/mailer/password_change.html.erb
create app/views/users/mailer/reset_password_instructions.html.erb
create app/views/users/mailer/unlock_instructions.html.erb
清单9. 定制化 views/users/sessions/new.html.erb<h2>Log in Custom</h2>
<%= form_for(resource, as: resource_name, url: session_path(resource_name)) do |f| %>
<div class="field"
<%= f.label :email %><br />
1
| <%= f.email_field :email, :class => "form-control input-sm", :autofocus => true %>
|
</div>
<div class="field">
<%= f.label :password %><br />
<%= f.password_field :password, :class => "form-control input-sm", autocomplete: "off" %>
</div>
<% if devise_mapping.rememberable? -%>
<div class="field">
<%= f.check_box :remember_me %>
<%= f.label :remember_me %>
</div>
<% end -%>
<div class="actions">
<%= f.submit "Log in", :class => "btn btn-sm btn-info" %>
</div>
<% end %>
<%= render "users/shared/links" %>
注意,还需要在 config/initializers/devise.rb 中配置 config.scoped_views = true。
图3. 定制化登录页面 |