devise Register one users sign up, but need two different register paths - ruby-on-rails

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.

Related

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

Modal (Devise) for Rails App

I Have an application in RoR and I need to make a login modal.
I´m using the gem devise.
sessions_controller.rb
class Users::SessionsController < Devise::SessionsController
respond_to :js
end
In the devise folder of the views, I have the sessions folder with 2 files: _new.html.erb and new.js.erb
_new.html.erb
<div class="modal-dialog modal-sm">
<div class="modal-content">
<div class="modal-header">
<button class="close" data-dismiss="modal">x</button>
<h2 class="dark">Log in</h2>
</div>
<div class="modal-body">
<%= form_for(resource, as: resource_name, url: session_path(resource_name)) do |f| %>
<div class="field">
<%= f.label :email %><br />
<%= f.email_field :email, autofocus: true %>
</div>
<div class="field">
<%= f.label :password %><br />
<%= f.password_field :password, autocomplete: "off" %>
</div>
<% if devise_mapping.rememberable? -%>
<div class="field">
<%= f.check_box :remember_me %>
<%= f.label :remember_me %>
</div>
<% end -%>
<div class="actions">
<%= f.submit "Log in", class: "btn btn-md btn-black-line" %>
</div>
<% end %>
<%= render "devise/shared/links" %>
</div>
</div>
</div>
new.js.erb
$('#modaldevise').html('<%= j render('new') %>');
$('#modaldevise').modal();
In my application.html.erb I use this code to use modal:
<div id="modaldevise" class="modal fade"></div>
Finally I have a side menu that have the login link:
<%= link_to 'Login', new_user_session_path, 'data-toggle' => "modal" , 'data-target' => "#modaldevise" %>
When I press the Login link, the background becomes darker and the modal is opened, but the information from the partial _new.html.erb is not showed.
What is wrong here?
You can avoid using new.js.erb and do something like this:
application.html.erb
<div id="modaldevise" class="modal fade">
<%= render 'your_path/new' %>
</div>
side menu
<%= link_to 'Sign in', '#', "data-toggle" => "modal", "data-target"=>"#modaldevise" %>

Rails Devise Login 401 Unauthorized

What I am trying to do
I am trying to set up the Authentication with Devise in the Session Controller, methods new and create.
I am using Rails 5 and I customized the View new.html.erb.
What is the problem
I am not able to authenticate successfully after signing out from the Account. The problem is connected with the sign_in_params hash, which is passed blank to the new method of the session_controller
Testing of the functionality
I conducted a test of the Login, by including a breakpoint in my Account::SessionsController, I noticed that the sign_in_params hash was blank, then the program exits with 401 Unauthorized.
8: def new
9: self.resource = resource_class.new(sign_in_params)
=> 10: clean_up_passwords(resource)
11: yield resource if block_given?
12: respond_with(resource, serialize_options(resource))
13: end
sign_in_params => {}
My Code
I was able by resetting my code to identify the cause of my problem. It is connected to the custom Devise View that I modified in app/views/devise/sessions/new.html.erb
This is the code:
<form class="login-form" action="index.html">
<div class="login-wrap">
<%= form_for(resource, as: resource_name, url: session_path(resource_name)) do |f| %>
<p class="login-img"><i class="icon_lock_alt"></i></p>
<div class="field">
</div>
<div class="input-group">
<span class="input-group-addon"><i class="icon_profile"></i></span>
<%= f.email_field :email, autofocus: true, :class => "form-control", :placeholder => "Username" %>
<!--<input type="text" class="form-control" placeholder="Username" autofocus>-->
</div>
<div class="input-group">
<span class="input-group-addon"><i class="icon_key_alt"></i></span>
<%= f.password_field :password, autocomplete: "off", :class => "form-control", :placeholder => "Password" %>
</div>
<% if devise_mapping.rememberable? -%>
<label class="checkbox">
<%= f.check_box :remember_me %>
<%= f.label :remember_me, :class => "remember_me_Label" %>
<span class="pull-right"> <%= link_to "Forgot Password?", {}, :id => "lostPw" %></span>
</label>
<% end -%>
<%= f.submit "Login", :class => "btn btn-primary btn-lg btn-block" %>
<%= link_to "Signup", new_account_registration_path, :method => :get, class: "btn btn-info btn-lg btn-block signuptext" %>
<% end %>
</div>
</form>
In the log, I have the following important informations, but no errors:
Started GET "/account/index.html?utf8=%E2%9C%93&authenticity_token=s1aA%2FNcZo1HIkF1qJOlrIAlM9rJM3y1ced8tQLiWnGsSugBVs0AYKVJR8QLa1I%2BT500sYu7H1%2BPQ2UwH3JG2ew%3D%3D&account%5Bemail%5D=test%40email.com&account%5Bpassword%5D=[FILTERED]&account%5Bremember_me%5D=0&commit=Login" for 127.0.0.1 at 2017-02-06 19:16:04 +0100 (pid:8311)
2017-02-06 19:16:04.557 [fyi] Processing by AccountController#index as HTML (pid:8311)
2017-02-06 19:16:04.557 [fyi] Parameters: {"utf8"=>"✓", "authenticity_token"=>"s1aA/NcZo1HIkF1qJOlrIAlM9rJM3y1ced8tQLiWnGsSugBVs0AYKVJR8QLa1I+T500sYu7H1+PQ2UwH3JG2ew==", "account"=>{"email"=>"test#email.com", "password"=>"[FILTERED]", "remember_me"=>"0"}, "commit"=>"Login"} (pid:8311)
2017-02-06 19:16:04.558 [fyi] Completed 401 Unauthorized in 1ms (ActiveRecord: 0.0ms) (pid:8311)
Following is the solution of the problem
Thanks
Fabrizio Bertoglio
Solution
The problem is in the html code of new.html.erb, as I did not remove the html <form> tags from the html and this would cause problems with my <%= form_for > <% end %> rails tag. This is the reason why strong_params is empty.
The form was not submitting any value to the controller, the problem could have been easily discovered by debugging directly the new.html.erb view instead of the controller.
Was helpful having the input from Max, that suggested me to restart all over with Devise, so that I could understand that the problem was connected with the form and not with rails strong parameters.
This is the correct new.html.erb.
<%= form_for(resource, as: resource_name, url: session_path(resource_name), html: { class: "login-form"}) do |f| %>
<div class="login-wrap">
<p class="login-img"><i class="icon_lock_alt"></i></p>
<div class="input-group">
<span class="input-group-addon"><i class="icon_profile"></i></span>
<%#= f.label :email %>
<%= f.email_field :email, autofocus: true, :class => "form-control", :placeholder => "Username" %>
<!--<input type="text" class="form-control" placeholder="Username" autofocus>-->
</div>
<div class="input-group">
<span class="input-group-addon"><i class="icon_key_alt"></i></span>
<%#= f.label :password %>
<%= f.password_field :password, autocomplete: "off", :class => "form-control", :placeholder => "Password" %>
</div>
<% if devise_mapping.rememberable? -%>
<label class="checkbox">
<%= f.check_box :remember_me %>
<%= f.label :remember_me, :class => "remember_me_Label" %>
<span class="pull-right"> <%= link_to "Forgot Password?", {}, :id => "lostPw" %></span>
</label>
<% end -%>
<%= f.submit "Login", :class => "btn btn-primary btn-lg btn-block" %>
<%= link_to "Signup", new_registration_path(resource_name), :method => :get, class: "btn btn-info btn-lg btn-block signuptext" %>
<div class="links ">
<%= render "devise/shared/links2" %>
</div>
</div>
<% end %>
<div class="text-right">
<div class="credits">
<!--
All the links in the footer should remain intact.
You can delete the links only if you purchased the pro version.
Licensing information: https://bootstrapmade.com/license/
Purchase the pro version form: https://bootstrapmade.com/buy/?theme=NiceAdmin
-->
Business Bootstrap Themes by BootstrapMade
</div>
</div>
</div>

Customize the devise edit method - Rails 4

I have a problem with customising Devise method. I want to edit an existing profile. But it doesn't change.
Here's my code
My controller:
class DashboardController < Users::RegistrationsController::ApplicationController
def index
end
def update
#user = current_user
if #user.update_attributes(user_params)
redirect_to 'index', :notice => "Successfully updated profile."
else
Rails.logger.info(#user.errors.messages.inspect)
render :action => 'edit'
end
end
def edit
end
private
def user_params
params.require(:user).permit(:avatar, :username, :email, :password, :password_confirmation)
end
end
My edit view
<div class="container sign-in-up">
<div class="row">
<div class="col-md-4 col-md-offset-4">
<br>
<div class="text-center">
<h1><b>Edit the profile</b> </h2>
<div class="tab-content">
<div class="tab-pane fade in active" id="new">
<fieldset>
<div class="form-group">
<div class="right-inner-addon">
<i class="fa fa-envelope"></i>
<%= form_for current_user, url: edit_dashboard_path(current_user), html: { method: :get, multipart: true } do |f|%>
<div class="home_field " >
<%= f.label :username%>
<br/>
<%= f.text_field :username, autofocus: true, :placeholder => 'Username', class:"form-control input-lg " %>
</div>
<div class="home_field ">
<%= f.label :email %>
<br/>
<%= f.email_field :email, autofocus: true, :placeholder => 'Email' , class:"form-control input-lg" %>
</div>
<div class="home_field ">
<%= f.label :password%>
<br/>
<%= f.password_field :password, :placeholder => 'Password', class:"form-control input-lg"%>
</div>
<div class="home_field ">
<%= f.label :password_confirmation%>
<br/>
<%= f.password_field :password_confirmation,:placeholder => 'Confirm your password' , class:"form-control input-lg"%>
</div>
<div class ="home_field">
<%= f.file_field :avatar %>
<br>
</div>
<div>
</div>
<div class="home_field">
<%= f.submit 'Edit the profile', class: "btn btn-primary btn-lg btn-block btn-group" %>
<br/>
</div>
<% end %>
</fieldset>
It can be a problem in my layout code, where i make the link to the edit page
<%= link_to "Edit the Profile", edit_dashboard_path %>
Your form is submitting to the edit dashboard using a get, which is the same page you are on. Instead you want to update the current user something like this:
<%= form_for current_user, html: { multipart: true } do |f|%>
This will default to the update attribute and CRUD action defined in your routes file for a user
I would suggest changing this line:
<%= form_for current_user, url: edit_dashboard_path(current_user), html: { method: :get, multipart: true } do |f|%>
In the url use your path to the 'update' method, not the edit (use rake routes on your command line)
In the method use :put instead of :get

Devise Error Messages not showing up Bootstrap template

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

Resources