简体   繁体   中英

Ruby on Rails: Devise gem, adding a “name” to a User model

So I have the original version of devise, where a user is authenticated through their email address. However, I want to add an option for a user to enter their name, which of course doesn't matter if it's not unique, it's just for displaying purposes; how do I customize the devise form? I have a :name attribute in my User model already, here is what the form looks like:

    <%= form_for(resource, as: resource_name, url: registration_path(resource_name)) do |f| %>

      # This field not working
      <div class="field">
        <%= f.text_field :name, autofocus: true, placeholder: "What is your nickname?" %>
      </div>


      <div class="field">
        <%= f.email_field :email, placeholder: "Enter your email address" %>
      </div>

      <div class="field">
        <%= f.password_field :password, autocomplete: "off",
            placeholder: "Enter your password" %>
      </div>

      <div class="field">
        <%= f.password_field :password_confirmation, autocomplete: "off",
            placeholder: "Confirm your password" %>
      </div>

      <div class="actions">
        <%= f.submit "Sign up" %>
      </div>
    <% end %>

Where am I going wrong? When a user signs up the name shows up as blank and it is not registered into the database...

I think your problem is related to Strong Params . Add this to your ApplicationController .

class ApplicationController < ActionController::Base
  before_action :configure_permitted_parameters, if: :devise_controller?

  protected

  def configure_permitted_parameters
    devise_parameter_sanitizer.for(:sign_up) << :user
  end
end

Source: https://github.com/plataformatec/devise#strong-parameters

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM