我正在使用两个Devise模型(Admin
和User
)进行Rails 5.2项目。我遵循了关于此Stack Overflow answer的建议,即在不破坏Devise的edit_admin_registration_path
的情况下禁用Devise模型的注册。
应用routes.rb
中建议的更改后,运行rails routes
显示更改按预期工作,但是,当要编辑Admin
(edit_admin_registration_path
)时,我得到了错误:
NoMethodError in Admins::RegistrationsController#edit
undefined method `validatable?' for nil:NilClass
我怀疑问题在于链接的答案未使用具有作用域视图和控制器的多个Devise模型。
有什么想法吗?
我已将其添加到routes.rb
:
devise_scope :admin do
get "/sign_in" => "admins/sessions#new" # custom path to login/sign_in
get "/sign_up" => "admins/registrations#new", as: "new_admin_registration" # custom path to sign_up/registration
end
devise_for :admins, skip: [ :registrations ], path: 'admins', controllers: { sessions: 'admins/sessions', registrations: 'admins/registrations' }
as :admins do
get 'admins/edit' => 'admins/registrations#edit', as: 'edit_admin_registration'
put 'admins' => 'admins/registrations#update', as: 'admin_registration'
end
查看表单:
<%= form_for(resource, as: resource_name, url: registration_path(resource_name), method: :put) do |f| %>
<%= devise_error_messages! %>
<%= f.label :email %><br />
<%= f.email_field :email, autofocus: true, autocomplete: "email", class: "form-control" %>
<% if devise_mapping.confirmable? && resource.pending_reconfirmation? %>
<div>Currently waiting confirmation for: <%= resource.unconfirmed_email %></div>
<% end %>
<%= f.label :password %> <i>(leave blank if you don't want to change it)</i><br />
<%= f.password_field :password, autocomplete: "off", class: "form-control" %>
<% if @minimum_password_length %>
<em><%= @minimum_password_length %> characters minimum</em>
<% end %>
<%= f.label :password_confirmation %><br />
<%= f.password_field :password_confirmation, autocomplete: "off", class: "form-control" %>
<%= f.label :current_password %> <i>(we need your current password to confirm your changes)</i><br />
<%= f.password_field :current_password, autocomplete: "off", class: "form-control" %>
<%= f.submit "Update", class: "btn btn-success" %>
<% end %>
答案 0 :(得分:1)
找到答案
需要像这样配置路由(可以在this Stack Overflow answer中找到):
devise_for :admins, :skip => [:registrations], path: 'admins', controllers: { sessions: 'admins/sessions', registrations: 'admins/registrations' }
devise_scope :admin do
get 'admins/edit' => 'admins/registrations#edit', as: 'edit_admin_registration'
put 'admins' => 'admins/registrations#update', as: 'admin_registration'
end