2016-01-16 5 views
6

Я пытаюсь разрешить пользователям изменять их адреса электронной почты, которые разрабатываются как уникальное имя пользователя. Несмотря на то, что обновление не дает ошибок, никаких изменений в адрес электронной почты для пользователя в базе данных не происходит.Утилита изменения электронной почты не обновляет адрес электронной почты

Ниже приведены соответствующие части кода:

Форма:

<%= f.fields_for :user_account, @user.user_account do |user_account| %> 
<p>Edit email address for account: <%= @user.user_account.email %></p> 
<div class="field"> 
    <%= user_account.label :new_email %><br /> 
    <%= user_account.text_field :email, autocomplete: "off", value: nil %> 
</div> 
<div class="field"> 
    <%= user_account.label :password %> <i>(please confirm the password associated with this account)</i><br /> 
    <%= user_account.password_field :current_password, autocomplete: "off" %> 
</div> 
<%= hidden_field_tag 'form', 'email' %> 
<div class="actions"> 
    <%= user_account.submit "Edit" %> 
</div> 

Контроллер:

def update 
respond_to do |format| 
if params[:form] == 'email' 
    if @user.user_account.valid_password?(params[:user][:user_account_attributes][:current_password]) 
     if @user.update(user_params) 
     format.html { redirect_to user_path(@user), :notice => 'your new email has been saved' } 
     format.json { render :show, status: :ok, location: @user } 
     else 
     format.html { render :edit } 
     format.json { render json: @user.errors, status: :unprocessable_entity } 
     end 
    else 
     format.html { redirect_to edit_email_path(@user), :notice => 'incorrect password (email)' } 
    end 
else ... 

метод user_params:

def user_params 
params.require(:user).permit(
    :first_name, :middle_initial, :last_name, 
    :linkedin, :website, :facebook, :video, :phone_number, 
    :address_1, :address_2, :city, :zip_code, 
    :image, :number, :years_practicing, :neighborhood, :biography, :price, :status, 
    user_employments_attributes: [:id, :user_id, :company, :position, :start_date, :end_date, :info, :_destroy], 
    user_educations_attributes: [:id, :user_id, :school, :degree_title, :start_date, :end_date, :info, :_destroy], 
    user_account_attributes: [:id, :user_id, :email, :password, :password_confirmation, :_destroy], 
    user_category_ids:[]) 
end 

учетная запись пользователя модель:

class UserAccount < ActiveRecord::Base 
    # Include default devise modules. Others available are: 
    # , :lockable, :timeoutable and :omniauthable 
    devise :database_authenticatable, :registerable, :confirmable, 
    :recoverable, :rememberable, :trackable, :validatable 
    belongs_to :user 
end 

ответ

0

Я не уверен, но я думаю, в модели пользователя должно быть

accepts_nested_attributes_for :user_account 
+0

yep, уже есть что :( – chris

5

Итак, получается, что новый адрес электронной почты был спасаемых как: unconfirmed_email, но это не повлияло на функциональность связанной учетной записи, так как у учетной записи все еще было старое электронное письмо, хранящееся как: электронная почта.

Таким образом, я должен был сделать

user.confirmed_at = nil 
user.save(:validate => false) 

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

+0

Я использовал вызов 'confirm()'. Такая же идея. –

Смежные вопросы