Разработать edit_registration теперь работает правильно - PullRequest
0 голосов
/ 16 октября 2019

Я пытаюсь отредактировать информацию о текущем пользователе, используя 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 %>

После того, как я нажму кнопку обновления, я перенаправлен на другую страницу и получаю флэш-сообщение с надписью «Ваша учетная запись была успешно обновлена».

Но информация вообще не обновляется, и отображается та же старая информация.

Дополнительная информация: Также у меня есть некоторые проверки в файле модели 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

1 Ответ

1 голос
/ 16 октября 2019

Очевидная проблема заключается в том, что вы не разрешили дополнительные параметры, которые просто игнорируются.

Обязательно прочитайте Разработать документацию , полностью.

Скорее всего исправьте ...

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 обрабатывает эту проверку для вас).

...