设计 edit_registration 现在可以正常工作
Devise edit_registration now working properly
我正在尝试使用edit_user_registration_path
编辑当前用户的信息。
这是edit_registration.html.erb
的代码
<h2>Edit <%= resource_name.to_s.humanize %></h2>
<%= form_for(resource, as: resource_name, url: registration_path(resource_name), html: { method: :put }) do |f| %>
<%= render "devise/shared/error_messages", resource: resource %>
<div class="field">
<%= f.label :first_name %><br />
<%= f.text_field :first_name, autofocus: true, autocomplete: "email" %>
</div>
<div class="field">
<%= f.label :last_name %><br />
<%= f.text_field :last_name, autofocus: true, autocomplete: "email" %>
</div>
<div class="field">
<%= f.label :contact_no %><br />
<%= f.text_field :contact_no, autofocus: true, autocomplete: "email" %>
</div>
<div class="field">
<%= f.label :address %><br />
<%= f.text_field :address, autofocus: true, autocomplete: "email" %>
</div>
<div class="field">
<%= f.label :email %><br />
<%= f.email_field :email, autofocus: true, autocomplete: "email" %>
</div>
<% if devise_mapping.confirmable? && resource.pending_reconfirmation? %>
<div>Currently waiting confirmation for: <%= resource.unconfirmed_email %></div>
<% end %>
<div class="field">
<%= f.label :password %> <i>(leave blank if you don't want to change it)</i><br />
<%= f.password_field :password, autocomplete: "new-password" %>
<% if @minimum_password_length %>
<br />
<em><%= @minimum_password_length %> characters minimum</em>
<% end %>
</div>
<div class="field">
<%= f.label :password_confirmation %><br />
<%= f.password_field :password_confirmation, autocomplete: "new-password" %>
</div>
<div class="field">
<%= f.label :current_password %> <i>(we need your current password to confirm your changes)</i><br />
<%= f.password_field :current_password, autocomplete: "current-password" %>
</div>
<div class="actions">
<%= f.submit "Update" %>
</div>
<% end %>
<h3>Cancel my account</h3>
<p>Unhappy? <%= button_to "Cancel my account", registration_path(resource_name), data: { confirm: "Are you sure?" }, method: :delete %></p>
<%= link_to "Back", :back %>
在我点击更新按钮后,我被重定向到另一个页面,我收到一条闪现消息 'Your account has been updated successfully.'
但是信息根本没有更新,显示的是同样的旧信息。
附加信息:
我在 user.rb
模型文件中也有一些验证,如:
validates :email, :password, presence: true, on: :create
validates :email, uniqueness: true
validates :password, confirmation: true, if: :password_present?
enum kind: {Admin: 0, Publisher: 1, Editor: 2}
def password_present?
password.present?
end
此文件用于路径 edit_organization_user_path
.
中用户的其他编辑页面
= form_for [current_organization,@user] do |f|
- if @user.errors.any?
#error_explanation
h2 = "#{pluralize(@user.errors.count, "error")} prohibited this organization from being saved:"
ul
- @user.errors.full_messages.each do |message|
li = message
.field
= f.label :first_name
= f.text_field :first_name
.field
= f.label :last_name
= f.text_field :last_name
.field
= f.label :contact_no
= f.text_field :contact_no
.field
= f.label :address
= f.text_field :address
.field
= f.label :email
= f.text_field :email
.field
= f.label :password
= f.password_field :password, autocomplete: 'disabled'
.field
= f.label :password_confirmation
= f.password_field :password_confirmation
.actions = f.submit
= link_to "Back", organizations_path
明显的问题是您不允许使用其他参数,这些参数只是被忽略了。
确保完整阅读 Devise Documentation。
最有可能修复...
class ApplicationController < ActionController::Base
before_action :configure_permitted_parameters, if: :devise_controller?
protected
def configure_permitted_parameters
devise_parameter_sanitizer.permit(:sign_up, keys: [:first_name,
:last_name,
:contact_no,
:address])
end
end
除此之外...您对 email
和 password
的用户验证是多余的(Devise 会为您处理此验证)。
我正在尝试使用edit_user_registration_path
编辑当前用户的信息。
这是edit_registration.html.erb
的代码<h2>Edit <%= resource_name.to_s.humanize %></h2>
<%= form_for(resource, as: resource_name, url: registration_path(resource_name), html: { method: :put }) do |f| %>
<%= render "devise/shared/error_messages", resource: resource %>
<div class="field">
<%= f.label :first_name %><br />
<%= f.text_field :first_name, autofocus: true, autocomplete: "email" %>
</div>
<div class="field">
<%= f.label :last_name %><br />
<%= f.text_field :last_name, autofocus: true, autocomplete: "email" %>
</div>
<div class="field">
<%= f.label :contact_no %><br />
<%= f.text_field :contact_no, autofocus: true, autocomplete: "email" %>
</div>
<div class="field">
<%= f.label :address %><br />
<%= f.text_field :address, autofocus: true, autocomplete: "email" %>
</div>
<div class="field">
<%= f.label :email %><br />
<%= f.email_field :email, autofocus: true, autocomplete: "email" %>
</div>
<% if devise_mapping.confirmable? && resource.pending_reconfirmation? %>
<div>Currently waiting confirmation for: <%= resource.unconfirmed_email %></div>
<% end %>
<div class="field">
<%= f.label :password %> <i>(leave blank if you don't want to change it)</i><br />
<%= f.password_field :password, autocomplete: "new-password" %>
<% if @minimum_password_length %>
<br />
<em><%= @minimum_password_length %> characters minimum</em>
<% end %>
</div>
<div class="field">
<%= f.label :password_confirmation %><br />
<%= f.password_field :password_confirmation, autocomplete: "new-password" %>
</div>
<div class="field">
<%= f.label :current_password %> <i>(we need your current password to confirm your changes)</i><br />
<%= f.password_field :current_password, autocomplete: "current-password" %>
</div>
<div class="actions">
<%= f.submit "Update" %>
</div>
<% end %>
<h3>Cancel my account</h3>
<p>Unhappy? <%= button_to "Cancel my account", registration_path(resource_name), data: { confirm: "Are you sure?" }, method: :delete %></p>
<%= link_to "Back", :back %>
在我点击更新按钮后,我被重定向到另一个页面,我收到一条闪现消息 'Your account has been updated successfully.'
但是信息根本没有更新,显示的是同样的旧信息。
附加信息:
我在 user.rb
模型文件中也有一些验证,如:
validates :email, :password, presence: true, on: :create
validates :email, uniqueness: true
validates :password, confirmation: true, if: :password_present?
enum kind: {Admin: 0, Publisher: 1, Editor: 2}
def password_present?
password.present?
end
此文件用于路径 edit_organization_user_path
.
= form_for [current_organization,@user] do |f|
- if @user.errors.any?
#error_explanation
h2 = "#{pluralize(@user.errors.count, "error")} prohibited this organization from being saved:"
ul
- @user.errors.full_messages.each do |message|
li = message
.field
= f.label :first_name
= f.text_field :first_name
.field
= f.label :last_name
= f.text_field :last_name
.field
= f.label :contact_no
= f.text_field :contact_no
.field
= f.label :address
= f.text_field :address
.field
= f.label :email
= f.text_field :email
.field
= f.label :password
= f.password_field :password, autocomplete: 'disabled'
.field
= f.label :password_confirmation
= f.password_field :password_confirmation
.actions = f.submit
= link_to "Back", organizations_path
明显的问题是您不允许使用其他参数,这些参数只是被忽略了。
确保完整阅读 Devise Documentation。
最有可能修复...
class ApplicationController < ActionController::Base
before_action :configure_permitted_parameters, if: :devise_controller?
protected
def configure_permitted_parameters
devise_parameter_sanitizer.permit(:sign_up, keys: [:first_name,
:last_name,
:contact_no,
:address])
end
end
除此之外...您对 email
和 password
的用户验证是多余的(Devise 会为您处理此验证)。