Config setting
I currently want to do a login using a program. In addition to email and password, I want to have a username. I created a migration:
class AddUsernameToBuyer < ActiveRecord::Migration
def change
add_column :buyers, :username, :string
add_index :buyers, :username, unique: true
end
end
I also added the username in the registration view:
<h2>Sign up</h2>
<%= form_for(resource, as: resource_name, url: registration_path(resource_name)) do |f| %>
<%= devise_error_messages! %>
<div class="field">
<%= f.label :username %> <br />
<%= f.text_field :username %>
</div>
<div class="field">
<%= f.label :email %><br />
<%= f.email_field :email, autofocus: true %>
</div>
<div class="field">
<%= f.label :password %>
<% if @validatable %>
<em>(<%= @minimum_password_length %> characters minimum)</em>
<% end %><br />
<%= f.password_field :password, autocomplete: "off" %>
</div>
<div class="field">
<%= f.label :password_confirmation %><br />
<%= f.password_field :password_confirmation, autocomplete: "off" %>
</div>
<div class="actions">
<%= f.submit "Sign up" %>
</div>
<% end %>
<%= render "devise/shared/links" %>
And I entered the app controller username as an allowed parameter:
class ApplicationController < ActionController::Base
# Prevent CSRF attacks by raising an exception.
# For APIs, you may want to use :null_session instead.
protect_from_forgery with: :exception
before_action :configure_permitted_parameters, if: :devise_controller?
protected
def configure_permitted_parameters
devise_parameter_sanitizer.for(:sign_up) { |u| u.permit(:username) }
end
def after_sign_up_path_for(resource)
redirect_to root_path
end
end
When I try to register, I redirect as I should: http://localhost:3000/buyers/sign_up
After I filled out the details, I clicked the register button and I want to be redirected to the root page. However, devise redirects me to http://localhost:3000/buyers
and it tells me that I cannot have a blank username even though I added it.
How can I change this behavior / fix it?
+3
source to share
2 answers