Usernames not getting saved on signup - ruby-on-rails

I'm using Devise to manager users and i'm getting a strange error. I have the categories of email, username, and password on my signup page, the email and password get passed to the database just fine but i can't get the username to save there. If i edit the user after signup, the username displays just fine.
Here is my signup page code:
<div class="panel panel-default">
<div class="panel-heading">
<h1>Sign up</h1>
</div>
<div class="panel-body">
<%= form_for(resource, :as => resource_name, :url => registration_path(resource_name)) do |f| %>
<%= devise_error_messages! %>
<div class="form-group">
<%= f.label :email %>
<%= f.email_field :email, autofocus: true, class: "form-control" %>
</div>
<div class="form-group">
<%= f.label :username %>
<%= f.text_field :username, class: "form-control" %>
</div>
<div class="form-group">
<%= f.label :password %>
<%= f.password_field :password, class: "form-control" %>
</div>
<div class="form-group">
<%= f.submit "Continue" %>
</div>
<% end %>
</div>
<div class="panel-footer">
<%= render "devise/shared/links" %>
</div>
</div>
Am i missing putting in username somewhere?

Solved this by adding
devise_parameter_sanitizer.for(:sign_up) { |u| u.permit(:username, :email, :password, :password_confirmation) }
to my applications controller

Related

Devise login always returns "Invalid Email or Password", even when they're right

I know it's apparently a frequent question (saw many replies here), but I couldn't fix it yet.
So.. I'm building a simple blog app with devise gem, but when I try to log in, I always get the "Invalid Email or password." message :/
Actually, this is my first real project with rails, so sometimes I'm kinda lost with routes, and etc... By the way, I would say the error is probably being caused by the routes file.
Please, I'd appreciate some help :)
My routes.rb file:
Rails.application.routes.draw do
root 'posts#index'
resources :posts do
resources :comments
end
devise_for :users
devise_scope :user do
get "login", :to => "devise/sessions#new"
get "logout", :to => "devise/sessions#destroy"
end
My sessions/new.html.erb file:
<div class="container mt-3 pb-3">
<div class="row">
<div class="col-lg-6 border">
<h1>Login</h1>
<%= form_for(resource, as: resource_name, url: session_path(resource_name)) do |f| %>
<div class="form-group">
<%= label_tag :email, "Email" %>
<%= text_field_tag :email, "", autofocus: true, :class =>"form-control" %>
</div>
<div class="form-group">
<%= label_tag :password, "Senha" %>
<%= password_field_tag :password, "", :class =>"form-control" %>
</div>
<div class="form-group">
<%= submit_tag "Entrar", :class =>"form-control btn btn-outline-dark" %>
</div>
<% end %>
<%= render "devise/shared/links" %>
</div>
</div>
</div>
My registrations/_form.html.erb:
<%= form_for(resource, as: resource_name, url: registration_path(resource_name)) do |f| %>
<%= render "devise/shared/error_messages", resource: resource %>
<div class="form-group">
<%= f.label :username, "Nome" %>
<%= f.text_field :username, autofocus: true, autocomplete: "username", :class =>"form-control" %>
</div>
<div class="form-group">
<%= f.label :email, "Email" %>
<%= f.email_field :email, autocomplete: "email", :class =>"form-control" %>
</div>
<div class="form-group">
<%= f.label :password, "Senha" %>
<% if #minimum_password_length %>
<em>(No mínimo <%= #minimum_password_length %> caracteres)</em>
<% end %><br />
<%= f.password_field :password, autocomplete: "new-password", :class =>"form-control" %>
</div>
<div class="form-group">
<%= f.label :password_confirmation, "Confirme sua senha" %><br />
<%= f.password_field :password_confirmation, autocomplete: "new-password", :class =>"form-control" %>
</div>
<div class="form-group">
<%= f.submit "Criar", :class =>"form-control btn btn-outline-dark" %>
</div>
<% end %>
JUST ANOTHER THING! MAYBE IT'S IMPORTANT...
I have a navbar rendered in "layouts/application.html.erb" that shows the current user's name and a logout button when there's a user connected, and a login button when there are no sessions.
<% unless current_page? (login_path) %>
<%= link_to 'Entrar', login_path, :class =>"btn btn-outline-light" %>
<% end %>
Your error was caused by wrong sign_in form.
I have changed this part
<%= form_for(resource, as: resource_name, url: session_path(resource_name)) do |f| %>
<div class="form-group">
<%= label_tag :email, "Email" %>
<%= text_field_tag :email, "", autofocus: true, :class =>"form-control" %>
</div>
<div class="form-group">
<%= label_tag :password, "Senha" %>
<%= password_field_tag :password, "", :class =>"form-control" %>
</div>
<div class="form-group">
<%= submit_tag "Entrar", :class =>"form-control btn btn-outline-dark" %>
</div>
<% end %>
To this one, and everything worked.
<%= form_for(resource, as: resource_name, url: session_path(resource_name)) do |f| %>
<div class="form-group">
<%= f.label :email, "Email" %>
<%= f.text_field :email, autofocus: true, :class =>"form-control" %>
</div>
<div class="form-group">
<%= f.label :password, "Senha" %>
<%= f.password_field :password, :class =>"form-control" %>
</div>
<div class="form-group">
<%= f.submit "Entrar", :class =>"form-control btn btn-outline-dark" %>
</div>
<% end %>

How to save additional informations from Devise form?

I have a application controller like this :
class ApplicationController < ActionController::Base
before_action :configure_permitted_parameters, if: :devise_controller?
protected
def configure_permitted_parameters
devise_parameter_sanitizer.permit(:sign_up, keys: [:first_name, :last_name, :description, :city_id, :golf_level])
end
end
and a form sign up with device like this :
<h2 class='container'>Inscription</h2>
<div class="tab-pane show active" id="component-1-1" role="tabpanel" aria-labelledby="component-1-1">
<div class="component-example">
<div class='container'>
<%= form_for(resource, as: resource_name, url: registration_path(resource_name)) do |f| %>
<div class='row'>
<div class="col-md-6">
<%= render "devise/shared/error_messages", resource: resource %>
<div class='field form-group'>
<%= f.label 'Prénom' %><br>
<%= f.text_field :first_name, class: 'form-control' %>
</div>
<div class='field form-group'>
<%= f.label 'Nom' %><br>
<%= f.text_field :last_name, class: 'form-control' %>
</div>
<div class="field form-group">
<%= f.label :email %><br />
<%= f.email_field :email, autofocus: true, autocomplete: "email", class: 'form-control' %>
</div>
<div class='field form-group'>
<%= f.label 'Niveau' %>
<%= select_tag(:golf_level, options_for_select([['débutant', 'débutant'], ['intermediaire', 'intermediaire'], ['confirmer', 'confirmer']])) %>
</div>
</div>
<div class="col-md-6">
<div class="field form-group">
<%= f.label 'Mot de passe' %>
<% if #minimum_password_length %>
<em>(<%= #minimum_password_length %> characters minimum)</em>
<% end %><br />
<%= f.password_field :password, autocomplete: "new-password", class: 'form-control' %>
</div>
<div class="field form-group">
<%= f.label 'Confirmer mot de passe' %><br />
<%= f.password_field :password_confirmation, autocomplete: "new-password", class: 'form-control' %>
</div>
<div class='field form-group'>
<%= f.label 'Ville' %>
<%= select_tag(:city_id, options_for_select( City.all.collect { |city| [city.name, city.id] }, {target_blank: true} )) %>
</div>
<div class='field form-group'>
<%= f.label 'Description'%>
<%= f.text_area :description, size: "60x5", class: 'form-control'%>
</div>
</div>
<div class="actions">
<%= f.submit "Inscription", class: 'btn btn-primary' %>
</div>
<% end %>
<button class='btn btn-primary' style="margin-left:20px;"><%= render "devise/shared/links" %></button>
</div>
</div>
</div>
</div>
My problem is maybe stupid but : When i create a User with this form the selects tags golf_level and city_id are not save in db but all other yes. In device doc I don't find special way for select form.
Ok, your parameters are passed, but not within proper namespace. Instead of select_tag you should use f.select method:
<%= f.select(:city_id, options_for_select(City.pluck(:name, :id), target_blank: true )) %>
And likewise for the second of your params.

Unable to create a user using a custom form using devise in ruby on rails

Im having a problem creating a devise user using a custom form. Whenever I submit the form I get an error saying the email and password fields cant be blank, It seems that the problem is something to do with the parameters not being permitted. I Have tried creating my own sign_up_params method to permit them manually but it still doesn't work and keep seeing the error.
Here is my code:
This is the form I am submitting:
<div class="panel panel-default">
<div class="panel-heading">
<h4>Sign Up</h4>
</div>
<div class="panel-body">
<%= form_for resource, url: user_registration_path do |f| %>
<%= devise_error_messages! %>
<div class="form-group row">
<%= f.label :email, "Email: ", class: "col-sm-2" %>
<div class="col-sm-10">
<%= f.email_field :email, class: 'form-control' %>
</div>
</div>
<div class="form-group row">
<%= f.label :password, "Password: ", class: "col-sm-2 col-form-label" %>
<div class="col-sm-10">
<%= f.password_field :password, class: 'form-control' %>
</div>
</div>
<div class="form-group row">
<%= f.label :password_confirmation, "Confirmation: ", class: "col-sm-2 col-form-label" %>
<div class="col-sm-10">
<%= f.password_field :password_confirmation, class: 'form-control' %>
</div>
</div>
<%= f.submit "Sign Up", class: 'btn btn-primary' %>
<% end %>
</div>
</div>
And this is the controller:
class Users::RegistrationsController < Devise::RegistrationsController
include DeviseHelper
def new
end
protected
def sign_up_params
params.require(:user).permit(:email, :password, :password_confirmation)
end
end
Also my routes file:
Rails.application.routes.draw do
root 'users/registrations#new'
devise_for :users, controllers: {registrations: 'users/registrations', sessions: 'users/sessions'}
end
This is really starting to bug me now as Ive set up devise plenty of times before and never had this issue. If anyone could help me out here it would be really appreciated

Devise: Submitting Devise forms seem to be using the GET method as opposed to POST, not logging in/signing up

I have just set up Devise for my User model. I tried to register using Devise, however the form is not submitting. When I press the "Register" button on my register page, the page refreshes and the submitted parameters appear in the URL of the page, as if Devise is submitting the form using GET as opposed to POST.
For example, this is what appears in the address bar of my browser after I press the 'Register' button.
http://localhost:3000/auth/login?utf8=%E2%9C%93&authenticity_token=YMjcWnx3RCpdZbkmfcky%2F0ob47fPTCM1pTrg%2Bq3k%2FWA%3D&user%5Bemail%5D=test#test.com&user%5Bpassword%5D=test&commit=Login
Here is the code for my Devise registration view (registrations/new.html.erb):
<div class="devise-form">
<div class="well">
<form class="form-horizontal">
<fieldset>
<legend>Register</legend>
<%= form_for(resource, as: resource_name, url: registration_path(resource_name)) do |f| %>
<div class="form-group">
<div class="col-lg-12">
<%= f.text_field :email, required: true, autofocus: true, class: 'form-control input-lg', placeholder: 'Email' %>
</div>
</div>
<div class="form-group">
<div class="col-lg-12">
<%= f.password_field :password, required: true, class: 'form-control input-lg', placeholder: 'Password' %>
</div>
</div>
<div class="form-group">
<div class="col-lg-12">
<%= f.password_field :password_confirmation, required: true, class: 'form-control input-lg', placeholder: 'Password confirmation' %>
</div>
</div>
<div class="form-group">
<div class="col-lg-12">
<%= f.submit 'Register', class: 'btn btn-primary' %>
</div>
</div>
<% end %>
</fieldset>
</form>
</div>
<%= render "devise/shared/links" %>
Thank you!
Figured it out, I had accidentally forgotten to remove the actual HTML tags that were surrounding the Rails form_for method.

Using Devise and Simple form

I am using devise in my rails app, I converted the devise views to use simple form. When I attempt to sign up with an incorrect password format the flash messages for the password do not appear. Flash messages appear for ever other field. Does anyone know how to fix this problem.
<div class="container">
<div class="panel small-5 small-centered columns">
<div class="row">
<h2>Sign Up</h2>
<%= simple_form_for(resource, :as => resource_name, :url => registration_path(resource_name),
:html => {:multipart => true}) do |f| %>
<%= f.input :email, label: "E-mail", :autofocus => true %>
</div>
<div class="row">
<div class="row">
<div class="small-6 columns">
<%= f.label :password, label: "Password" %>
<%= f.password_field :password %>
</div>
<div class="small-6 columns">
<%= f.label :password_confirmation %>
<div id="password-confirm">
<%= f.password_field :password_confirmation %>
</div>
</div>
</div>
</div>
<div class="row">
<div class="row">
<div class="small-6 columns">
<%= f.input :last_name, label: "First Name" %>
</div>
<div class="small-6 columns">
<%= f.input :first_name, label: "Last Name" %>
</div>
</div>
</div>
<div class="row">
<div class="sign-up">
<%= f.label "Profile Photo" %><br>
<%= f.file_field :avatar, label: "Profile Photo" %>
<%= f.input :twitter_handle, label: "Twitter Url" %>
<%= f.input :linked_in_url, label: "Linked In Url" %>
<%= f.input :about_me, label: "About You (500 character max)" %>
<%= f.submit "Sign up", class: "button" %>
<% end %>
</div>
</div>
</div>
</div>

Resources