Devise Error Messages not showing up Bootstrap template - ruby-on-rails

I am using a Wrapbootstrap downloaded template, with devise for user registration and log in but am not getting any errors to pop up.
In all my views for devise I am calling <%= devise_error_messages! %>
In my model the following validations:
validates :first_name, presence: true
validates :last_name, presence: true
validates :phone_number, presence: true
Here is my Devise_helper.rb
module DeviseHelper
def devise_error_messages!
return '' if resource.errors.empty?
messages = resource.errors.full_messages.map { |msg| content_tag(:li, msg) }.join
html = <<-HTML
<div class="alert alert-error alert-block"> <button type="button"
class="close" data-dismiss="alert">x</button>
#{messages}
</div>
HTML
html.html_safe
end
end
Here is my Registration New view page:
<!--=== Content Part ===-->
<div class="container content">
<div class="row">
<div class="col-md-6 col-md-offset-3 col-sm-8 col-sm-offset-2">
<form class="reg-page">
<div class="reg-header">
<h2>Register a new account</h2>
<p>Already Signed Up? Click <%= link_to "Sign In", login_url %> to login your account.</p>
</div>
<%= form_for(resource, as: resource_name, url: registration_path(resource_name)) do |f| %>
<%= devise_error_messages! %>
<label>First Name<span class="color-red">*</span></label>
<%= f.text_field :first_name, autofocus: true, :class=> "form-control margin-bottom-20" %>
<label>Last Name<span class="color-red">*</span></label>
<%= f.text_field :last_name, autofocus: true, :class=> "form-control margin-bottom-20" %>
<label>Phone Number<span class="color-red">*</span></label>
<%= f.text_field :phone_number, autofocus: true, :class=> "form-control margin-bottom-20" %>
<label>Email Address <span class="color-red">*</span></label>
<%= f.email_field :email, autofocus: true, :class=> "form-control margin-bottom-20" %>
<div class="row">
<div class="col-sm-6">
<label>Password <span class="color-red">*</span></label>
<% if #validatable %>
<em>(<%= #minimum_password_length %> characters minimum)</em>
<% end %>
<%= f.password_field :password, autocomplete: "off", :class=> "form-control margin-bottom-20" %>
</div>
<div class="col-sm-6">
<label>Confirm Password <span class="color-red">*</span></label>
<%= f.password_field :password_confirmation, autocomplete: "off",:class=> "form-control margin-bottom-20" %>
</div>
</div>
<hr>
<div class="row">
<div class="col-lg-6 text-right">
<%= f.submit "Sign up", :class=> "btn-u" %>
</div>
</div>
</form>
</div>
</div>
<% end %>
</div><!--/container-->
<!--=== End Content Part ===-->
And when I submit an uncomplete form a query string like this is generated: http://localhost:3000/login?utf8=%E2%9C%93&authenticity_token=v%2BKndd3om3fBChpsZ6riw%2FVBiVM1ehVbKgBF9tIlg6PB6p30oei7OU3toqthjU1TY67yI3gmzjigxGe5DWfX7w%3D%3D&user%5Bemail%5D=&user%5Bpassword%5D=fwf&user%5Bremember_me%5D=0&commit=Log+in

Try with this in your helper file:
message_html << "<div class="alert alert-error alert-block"> <button type="button" class="close" data-dismiss="alert">x</button>"
message_html.html_safe
Also, check for flash message in your layout page or corresponding login page as:
<% if flash[:alert] %>
<span class='flash_error'> <%= flash[:alert] %></span>
<% end %>

There reason for this was because the template I was using had its own Javascript for error messages that was overriding the error messages of devise. Both error messages were not showing up until I removed the JS included in the Wrapbootstrap template and it works now

Related

devise Register one users sign up, but need two different register paths

As i been work for this project for while, as this one was not on the scopes, until yesterday, as the clients and project manager change mind as they want two different signup as it was one single sign up before but now want change two, but i was thinking it possible use same one user, but different path
<div class="row">
<div class="col-md-4 col-md-offset-4">
<h2 class="text-center">Sign up</h2>
</div>
<div class="col-sm-offset-4 col-sm-4 margin-button-bottom">
<div class="col-sm-12 text center">
<div class="inner-addon right-addon">
<i class="custom custom-icon-arrow-right"></i>
<%= link_to "I want hire Equipment", new_user_registration_path, class: "btn btn-black-free-account btn-lg btn-block", role:"button" %>
</div>
</div>
</div>
<div class="col-sm-offset-4 col-sm-4 margin-button-bottom">
<div class="col-sm-12 text center">
<div class="inner-addon right-addon">
<i class="custom custom-icon-arrow-right"></i>
<p><%= link_to "I represent a business with equipment available for hire", new_user_registration_path, class: "btn btn-black-free-account btn-lg btn-block", role:"button" %></p>
</div>
</div>
</div>
<br/>
so if this button "I represent a business with equipment available for hire" show form same as user but add field is Company
Here is two different
devise register new.html.erb
<div class="row">
<div class="col-md-4 col-md-offset-4">
<h2 class="text-center">Sign up</h2>
<br />
<%= form_for(resource, as: resource_name, url: registration_path(resource_name)) do |f| %>
<%= render 'shared/regmessage' %>
<div class="form-group">
<%= f.text_field :fullname, autofocus: true, placeholder: "Full name", class: "form-control", autocomplete: "fullname" %>
</div>
<div class="form-group">
<%= f.email_field :email, autofocus: true, placeholder: "Email", class: "form-control", autocomplete: "email" %>
</div>
<div class="form-group">
<% if #minimum_password_length %>
<em>(
<%= #minimum_password_length %> characters minimum)</em>
<% end %><br />
<%= f.password_field :password, placeholder: "Password", class: "form-control", autocomplete: "new-password" %>
</div>
<div class="actions">
<%= f.submit "Sign up", class: "btn btn-black-free-account btn-block" %>
</div>
<% end %>
<hr />
<%= link_to user_facebook_omniauth_authorize_path, class: "btn btn-block btn-social btn-facebook" do %>
<span class="fa fa-facebook"></span> Sign in with Facebook
<% end %>
<hr />
<%= link_to user_google_oauth2_omniauth_authorize_path, class: "btn btn-block btn-social btn-google" do %>
<span class="fa fa-google"></span> Sign in with Google
<% end %>
<br/>
</div>
</div>
same as above but add is company field in the new_company.html.erb
<div class="row">
<div class="col-md-4 col-md-offset-4">
<h2 class="text-center">Sign up</h2>
<br />
<%= form_for(resource, as: resource_name, url: registration_path(resource_name)) do |f| %>
<%= render 'shared/regmessage' %>
<div class="form-group">
<%= f.text_field :company, autofocus: true, placeholder: "Company Name", class: "form-control", autocomplete: "Company-name" %>
</div>
<div class="form-group">
<%= f.text_field :fullname, autofocus: true, placeholder: "Full name", class: "form-control", autocomplete: "fullname" %>
</div>
<div class="form-group">
<%= f.email_field :email, autofocus: true, placeholder: "Email", class: "form-control", autocomplete: "email" %>
</div>
<div class="form-group">
<% if #minimum_password_length %>
<em>(
<%= #minimum_password_length %> characters minimum)</em>
<% end %><br />
<%= f.password_field :password, placeholder: "Password", class: "form-control", autocomplete: "new-password" %>
</div>
<div class="actions">
<%= f.submit "Sign up", class: "btn btn-black-free-account btn-block" %>
</div>
<% end %>
<br/>
<%= link_to user_facebook_omniauth_authorize_path, class: "btn btn-block btn-social btn-facebook" do %>
<span class="fa fa-facebook"></span> Sign in with Facebook
<% end %>
<br/>
<%= link_to user_google_oauth2_omniauth_authorize_path, class: "btn btn-block btn-social btn-google" do %>
<span class="fa fa-google"></span> Sign in with Google
<% end %>
<br/>
</div>
</div>
Route.rb
devise_for :users,
path: '',
path_names: {sign_in: 'login', sign_out: 'logout', edit: 'profile', sign_up: 'registration'},
:controllers => {omniauth_callbacks: 'omniauth_callbacks', registrations: 'registrations', }
register controller.rb
class RegistrationsController < Devise::RegistrationsController
protected
def update_resource(resource, params)
resource.update_without_password(params)
end
end
I was thinking something like java-script or controller if i click business button then the route will know and add company field into new.html.erb rather decided to go which one of two new and new company.html.erb
maybe new ID in the new.html.erb or need configure at route or controller?
In your registrations controller set a flag variable #for_company for example based on the params[:for_company] presence:
class RegistrationsController < Devise::RegistrationsController
def new
#for_company = params[:for_company].present?
super
end
end
And in your Sign Up page simply add for_company: true param to a link to a company sign up:
<%= link_to "I represent a business with equipment available for hire", new_user_registration_path(for_company: true), class: "..." %>
Then just show :company field in case if #for_company is true.

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.

devise Invalid Email or password

After adding styling to the devise pages, the logins are not working.
Signup redirects to login and login says Invalid Email or password.
Here is the sessions/new
<%= form_for(resource, as: resource_name, url: session_path(resource_name)) do |f| %>
<%= devise_error_messages! %>
<div class="access_social">
<%- if devise_mapping.omniauthable? %>
<%- resource_class.omniauth_providers.each do |provider| %>
<%= link_to "Sign in with #{OmniAuth::Utils.camelize(provider)}", omniauth_authorize_path(resource_name, provider), class: "button button--facebook button--full-width" %>
<% end -%>
<% end -%>
</div>
<div class="oauth__divider"><span>OR</span></div>
<div class="form-group">
<%= f.label :email %>
<br />
<%= f.email_field :email, autofocus: true, autocomplete: "email", class: "form-control" %>
</div>
<div class="form-group">
<%= f.label :password %>
<br />
<%= f.password_field :password, autocomplete: "off", class: "form-control" %>
</div>
<div class="clearfix mb-2">
<div class="form-check">
<%= f.check_box :remember_me,class: "form-check-input", as: :boolean if devise_mapping.rememberable? %>
<label class="form-check-label">Remember me</label>
</div>
</div>
<div class="actions">
<%= f.submit "Sign in", class: "button button--primary button--full-width" %>
</div>
<div class="text-center mt-2">
<%- if devise_mapping.recoverable? && controller_name != 'passwords' && controller_name != 'registrations' %>
<%= link_to "Forgot your password?", new_password_path(resource_name) %>
<br />
<% end -%>
</div>
<% end %>
Controllers routes and config have not been touched since changing the UI either

Devise: Forbidden Attributes Error on Sign Up

I have set up devise. I added database authentication, only in the first instance. I was able to fill out the form and add it to my db. I then added omniauth and now, when I try to register using the form, rather than the social media options, I get a forbidden attributes error.
My view is:
<% if user_signed_in? %>
<li>
<%= link_to('Edit registration', edit_user_registration_path) %>
</li>
<% else %>
<li>
<%= link_to 'Register', new_user_registration_path, :remote => true, 'data-toggle' => "modal", 'data-target' => "#myModal", :method => 'get' %>
</li>
<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
<h4 class="modal-title" id="myModalLabel">Request an invite</h4>
</div>
<div class="modal-body">
<%- if devise_mapping.omniauthable? %>
<div class="facebookauth"> <%= link_to "Register with Facebook", user_omniauth_authorize_path(:facebook) %></div>
<br />
<% end -%>
<%- if devise_mapping.omniauthable? %>
<div class="linkedinauth"> <%= link_to "Register with LinkedIn", user_omniauth_authorize_path(:linkedin) %></div>
<br />
<% end -%>
</div>
<div class="modal-footer">
<%= simple_form_for(resource, as: resource_name, url: registration_path(resource_name)) do |f| %>
<%= f.error_notification %>
<div class="form-inputs" style="padding-left:15%; text-align:left;">
<%= f.input :first_name, required: true, autofocus: true %>
<%= f.input :last_name, required: true, autofocus: true %>
<%= f.input :email, required: true, autofocus: true %>
<%= f.input :password, required: true %>
<%= f.input :password_confirmation, required: true %>
</div>
<div class="form-actions" style="padding-left:15%; padding-top:5%; text-align:left;">
<%= f.button :submit, "Register" %><% end %>
</div>
</div>
</div>
</div>
</div>
<% end %>
My application controller has this:
def configure_permitted_parameters
devise_parameter_sanitizer.for(:sign_up) { |u| u.permit( :first_name, :last_name, :email, :password, :password_confirmation) }
devise_parameter_sanitizer.for(:sign_in) { |u| u.permit( :email, :password, :remember_me) }
end
I have just tried adding this to my user/registrations_controller, but it does not help.
def user_params
params.require(:user).permit(:first_name, :last_name, :email, :password, :password_confirmation )
end
I'm wondering if its because I don't have fields in my database for password or password confirmation? I have 'encrypted password'.
Any help would be very much appreciated.
Thank you

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