2017-09-23 7 views
0

私はdevise、ユーザー、およびプロファイルの2つのモデルセットアップをdevise 4.2で持っています。 Rails 4.0 with Devise. Nested attributes Unpermited parameters入れ子になった属性は、deviseの登録可能なカスタムビューでパスワードなしで更新します

私の登録/ edit.html.hamlビューで、ユーザーがフォームを送信すると、デフォルトで現在のパスワードを入力する必要があります。ただし、ネストされた属性が編集され、パスワードが指定されていない場合でも、フォームはネストされた属性を更新します。これを防ぐ方法を教えてください。

registrations_controller.rb:

class RegistrationsController < Devise::RegistrationsController 
before_filter :configure_permitted_parameters 

def new 
    build_resource({}) 
    self.resource.profile = Profile.new 
    respond_with self.resource 
end 

protected 


def configure_permitted_parameters 
    devise_parameter_sanitizer.permit(:account_update) do |u| 
     u.permit(<user fields>, profile_attributes: [<:profile_fields>]) 
    end 
    devise_parameter_sanitizer.permit(:sign_up) do |u| 
     u.permit(<user_fields>, profile_attributes: [<profile_fields>]) 
    end 
end 

エンド

登録/ edit.html.haml:

.authform 
    %h3 
    Edit #{resource_name.to_s.humanize} 
    = form_for(resource, :as => resource_name, :url => user_registration_path, :html => { :method => :patch, :role => 'form'}) do |f| 
    = devise_error_messages! 
    .form-group 
     = f.label :email 
     = f.email_field :email, class: 'form-control' 
     - if devise_mapping.confirmable? && resource.pending_reconfirmation? 
     %div 
      Currently waiting confirmation for: #{resource.unconfirmed_email} 
    %fieldset 
     %p Leave these fields blank if you don't want to change your password. 
     .form-group 
     = f.label :password 
     = f.password_field :password, :autocomplete => 'off', class: 'form-control' 
     .form-group 
     = f.label :password_confirmation 
     = f.password_field :password_confirmation, class: 'form-control' 
    %fieldset 
     = f.fields_for :profile do |profile_fields| 
     .form-group 
      = profile_fields.label :a_field_1 
      = profile_fields.number_field :a_field_1, min: 0, max: 8 
     .form-group 
      = profile_fields.label :a_field_2 
      = profile_fields.number_field :a_field_2, min: 0, max: 8 
     .form-group 
      = profile_fields.label :a_field_1 
      = profile_fields.text_field :a_field_2, class: 'form-control' 
    %fieldset 
     %p You must enter your current password to make changes. 
     .form-group 
     = f.label :current_password 
     = f.password_field :current_password, class: 'form-control' 

    = f.submit 'Update', :class => 'button right' 

答えて

0

それは根本的な原因は、メソッドassign_attributesレールへの内部呼び出したことが判明パスワードが無効であってもネストされた属性が割り当てられます。修正は、パスワードが有効であり、そうでない場合は、割り当てプロセスを停止したかどうかを確認する次のコード

def update_resource(resource, params)incorrect 
    unless resource.valid_password?(params[:current_password]) 
    resource.errors.add(:current_password, params[:current_password] ? :blank : :invalid) 
    return resource 
    end 

    resource.update_with_password(params) 
end 

で登録コントローラにupdate_resource方法を-乗る上にいました。

関連する問題