Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails/Devise/Bootstrap: Can't access @minimum_password_length in sign up modal

I've created a Bootstrap modal for Devise sign up, which is accessible via a link on the landing page navbar. The modal is working properly, i.e. creating a user. But when I try to add the password length hint to the password input - nothing. I checked the value of the Devise instance variable @minimum_password_length and it is nil. Any suggestions?

Rails 4.2.6, Ruby 2.3.0, Bootstrap 4.0.0.alpha3, Devise 4.0.0, Simple Form 3.2.1

/config/initializers/devise.rb

...
if Rails.env.test? || Rails.env.development?
  config.password_length = 2..128
else
  config.password_length = 8..128
end
...

/db/migrate/20160417123456_devise_create_users.rb

...
## Confirmable
t.string   :confirmation_token
t.datetime :confirmed_at
t.datetime :confirmation_sent_at
t.string   :unconfirmed_email # Only if using reconfirmable
...
add_index :users, :confirmation_token,   unique: true
...

/app/models/user.rb

...
devise :confirmable,
       :database_authenticatable,
       :registerable,
       :recoverable,
       :rememberable,
       :trackable,
       :validatable
...

/views/shared/_navbar.html.haml

...
%li.nav-item.pull-xs-right
  = link_to 'Sign Up', '#signUpModal', data: { toggle: 'modal', target: '#signUpModal' }, id: 'sign-up-nav-link', class: 'account-nav-link nav-link'

%li.nav-item.pull-xs-right
  = link_to 'Log In', '#logInModal', data: { toggle: 'modal', target: '#logInModal' }, id: 'log-in-nav-link', class: 'account-nav-link nav-link'

/app/views/shared/_sign_up_modal.html.haml

#signUpModal.modal.fade{ tabindex: '-1', role: 'dialog', aria: { labelledby: 'signUpModalLabel', hidden: 'true' } }
  .modal-dialog{ role: 'document' }
    .modal-content

      .modal-header
        %button.close{ type: 'button', data: { dismiss: 'modal' }, aria: { label: 'Close' } }
          %span{ aria: { hidden: 'true' } }
            %i.fa.fa-times-circle
        %h4#signUpModalLabel.modal-title Sign Up

      = simple_form_for resource, as: resource_name, url: registration_path(resource_name) do |f|

        .modal-body

          = f.error_notification

          %fieldset.form-group
            = f.input :email,                 label:        'Email Address',
                                              required:     'true',
                                              autocomplete: 'off',
                                              input_html:  { id: 'email-input', class: 'form-control' }

          %fieldset.form-group
            = f.input :password,              label:        'Password',
                                              required:     'true',
                                              autocomplete: 'off',
                                              input_html:  { class: 'form-control' },
                                              hint:        ('Min. length of #{ @minimum_password_length }' if @validatable)
          %fieldset.form-group
            = f.input :password_confirmation, label:        'Password Confirmation',
                                              required:     'true',
                                              autocomplete: 'off',
                                              input_html:  { class: 'form-control' }

          %fieldset.form-group
            = f.input :subdomain,             label:        'Subdomain',
                                              required:     'true',
                                              autocomplete: 'off',
                                              input_html:  { class: 'form-control' }

          - if devise_mapping.rememberable?
            = f.input :remember_me,           label:         'Remember me',
                                              as:            :boolean,
                                              boolean_style: :inline

        .modal-footer
          .actions
            = link_to 'Cancel', 'javascript:;', data: { dismiss: 'modal' }, class: 'btn btn-sm btn-secondary'
            = f.button :submit, 'Sign Up', disable_with: 'Creating account...', class: 'btn btn-sm btn-primary'

/app/helpers/application_helper.rb

def resource_name
  :user
end

def resource
  @resource ||= User.new
end

def devise_mapping
  @devise_mapping ||= Devise.mappings[:user]
end

/config/routes.rb

root to: 'landing_page#index'
devise_for :users
...
like image 715
eggroll Avatar asked Apr 21 '16 22:04

eggroll


1 Answers

I just solved this problem, by handing the @minimum_password_length to the view in the views controller function. You can access the minimum_password_length using your devise model. In my case it's the User model. In Code:

@minimum_password_length = User.password_length.min
like image 63
Ekkstein Avatar answered Sep 28 '22 08:09

Ekkstein