Devise и StrongParams — разрешение неопределенного метода для nil: NilClass

В настоящее время я использую Rails 4 и перемещаю приложение из attr_accessible в StrongParams. Итак, я настроил контроллер Devise:

class UsersController < Devise::RegistrationsController
  load_and_authorize_resource

  #...

  def update
    unless @user.userable?
      if params[:selected_person_type] ==  I18n::t('activerecord.attributes.user.individual')
        redirect_to new_individual_path and return
      elsif params[:selected_person_type] == I18n::t('activerecord.attributes.user.legal_entity')
        redirect_to new_legal_entity_path and return
      end
    end

    @avatar = params[:avatar_id].present? ? Avatar.find_by(id: params[:avatar_id]) : @user.avatar

    if params[:user][:password].blank?
      if @user.update_without_password(user_params)
        notice = if @user.unconfirmed_email.present? && Date.today == @user.confirmation_sent_at.to_date
          t('devise.confirmations.send_instructions')
        else
          t('views.messages.notices.personal_data_updated')
        end
        redirect_to edit_user_path(@user), notice: notice and return
      end
    else
      if @user.valid_password?(params[:user][:current_password])
        params[:user].delete("current_password")
        if @user.update_attributes(user_params) && @user.save
          sign_in(@user, bypass: true)
          redirect_to edit_user_path(@user), notice: t('views.messages.notices.personal_data_updated') and return
        end
      else
        @user.errors.add(:current_password, :invalid)
      end
    end

    render action: "edit"
  end

  def create
    if resource.save
      SystemMailer.send_mail(to: resource.email, body: resource.temp_password, subject: I18n.t('mailers.password.new')).deliver if resource.generate_password == '1'

      if request.xhr?
        expire_data_after_sign_in!
        respond_to do |format|
          format.js
        end
      else
        super
      end
    else
      if request.xhr?
        clean_up_passwords(resource)
        respond_to do |format|
          format.js
        end
      else
        super
      end
    end
  end

  private

  def user_params
    if current_user.present?
      params.require(:user).permit(:fullname, :about, :username, :email, :current_password, :password, :password_confirmation)
    end
  end
end

Я получил ошибку раньше:

Failure/Error: post :create, user: attributes_for(:unconfirmed_user)
     ActiveModel::ForbiddenAttributesError:
       ActiveModel::ForbiddenAttributesError

Это связано с тем, что CanCan не так совместим с StrongParams, поэтому я попробовал это исправление в ApplicationController:

class ApplicationController < ActionController::Base
  include SimpleCaptcha::ControllerHelpers
  include CaptchaHelper

  # Prevent CSRF attacks by raising an exception.
  # For APIs, you may want to use :null_session instead.
  protect_from_forgery with: :exception

  before_filter :cancan_workaround

  rescue_from CanCan::AccessDenied do |e|
    redirect_to '/', flash: { error: I18n.t('views.messages.notices.access_denied') }
  end

  private

  def cancan_workaround
    resource = controller_name.singularize.to_sym
    method = "#{resource}_params"
    params[resource] &&= send(method) if respond_to?(method, true)
  end
end

После этого исправления я получил эту ошибку:

UsersController should successfully create user
     Failure/Error: post :create, user: attributes_for(:unconfirmed_user)
     NoMethodError:
       undefined method `permit' for nil:NilClass
     # ./app/controllers/users_controller.rb:75:in 'create'
     # ./spec/controllers/users_controller_spec.rb:10:in `block (3 levels) in <top (required)>'
     # ./spec/controllers/users_controller_spec.rb:9:in `block (2 levels) in <top (required)>'

Вот ./app/controllers/users_controller.rb:75:in 'create' это super вызов в действии. Есть идеи, как это исправить?


person ExiRe    schedule 29.01.2014    source источник


Ответы (2)


Разработка

Devise работает не так, как другие системы ввода Rails — он использует собственные внутренние функции для обработки параметров. Похоже, что devise автоматически разрешает параметры email и password, и ваша задача — добавить остальные параметры parameter_sanitizer.

Разработка и сильные параметры

Суть в том, что, согласно документации Devise, вам придется использовать что-то как это:

#app/controllers/application_controller.rb
class ApplicationController < ActionController::Base
  before_filter :configure_permitted_parameters, if: :devise_controller?

  protected

  def configure_permitted_parameters
    devise_parameter_sanitizer.for(:sign_up) << :username
  end
end
person Richard Peck    schedule 29.01.2014

Ладно, я понял. Проблема была не в Devise, а в моих разрешениях:

  private

  def user_params
    if current_user.present?
      params.require(:user).permit(:fullname, :about, :username, :email, :current_password, :password, :password_confirmation)
    end
  end

Тест оценивался для неподписанного пользователя, поэтому, когда хук канкана пытался выполнить этот метод:

params[resource] &&= send(method) if respond_to?(method, true)

Он получил ноль, потому что пользователь не вошел в систему, поэтому хук преобразовал :user => { ... } в :user => nil. Итак, я исправил это, удалив current_user.present?.

  private

  def user_params
    params.require(:user).permit(:fullname, :about, :username, :email, :current_password, :password, :password_confirmation)
  end

Не уверен, насколько хорошо это решение безопасно.

person ExiRe    schedule 29.01.2014