I have two models which is Members and Company...
Members is the devise model... When I sign up I want to include the following fields in the signup form.
Name
Email address
Password
Company name (from Company model)
company type (from Company model)
Member has one company
I am trying to create the signup form via nested form.. But I am not sure to build the form for the Company to receive input from the user...
Here is my controller
class Brands::Members::RegistrationsController < Devise::RegistrationsController
before_action :configure_sign_up_params, only: [:create]
# before_action :configure_account_update_params, only: [:update]
# GET /resource/sign_up
def new
#company = Company.new
super
end
# POST /resource
def create
#company = Company.new(configure_sign_up_params)
#company.valid?
super
end
end
Here is my 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 :name %><br />
<%= f.text_field :name, autofocus: true %>
</div>
<div class="field">
<%= f.label :email %><br />
<%= f.email_field :email %>
</div>
<div class="field">
<%= f.label :password %>
<% if #minimum_password_length %>
<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>
-->
<%= #company.errors %>
<%= fields_for #company do |fc| %>
<div class="field">
<%= fc.label :name %><br />
<%= fc.text_field :name %>
</div>
<% end %>
<div class="actions">
<%= f.submit "Sign up" %>
</div>
<% end %>
<%= render "brands/members/shared/links" %>
Hope this will help,
Change your new action to this
#member = Member.new
#member.build_company
Related
This is application Controller for devise
class ApplicationController < ActionController::Base
before_action :configure_permitted_parameters, if: :devise_controller?
private
def configure_permitted_parameters
devise_parameter_sanitizer.permit(:sign_up, keys: [:role])
end
end
signup form from devise and adding role
<h2>Sign up</h2>
<%= form_for(resource, as: resource_name, url: registration_path(resource_name)) do |f| %>
<%= render "devise/shared/error_messages", resource: resource %>
<div class="field">
<%= f.label :email %><br />
<%= f.email_field :email, autofocus: true, autocomplete: "email" %>
</div>
<div class="field">
<%= f.label :password %>
<% if #minimum_password_length %>
<em>(<%= #minimum_password_length %> characters minimum)</em>
<% end %><br />
<%= f.password_field :password, autocomplete: "new-password" %>
</div>
<div class="field">
<%= f.label :password_confirmation %><br />
<%= f.password_field :password_confirmation, autocomplete: "new-password" %>
</div>
This is a role code
There is a problem when user select buyer on signup page it should go the homepage , when user select seller it should go to the selled dashboard
<%= f.label :role %>
<%= f.select :role, User.roles.keys %>
Here is the submit button
<div class="actions">
<%= f.submit "Sign up", data: {turbo: false} %>
</div>
<% end %>
<%= render "devise/shared/links" %>
You didn't provide much context about what you do in your controller when a user is signed up and how you build the form. Therefore I will just assume that it is a very basic form and all attributes are nested properly in the request and that the create action follows a simple scaffolding structure.
Then you basically only need to change where to redirect the user to depending on its role after the record was created.
# in controllers/users.rb
def create
user = User.new(user_params)
if user.save
case user.role
when 'user'
redirect_to root_path
when 'seller'
redirect_to dashboard_path
end
else
render :edit
end
end
Preamble
As a little bit of a setup to the problem, we have an authenticatable user model that is separate from the user model that contains non-authentication related data.
Problem
As the title suggests, adding custom fields to the built in configure_sign_up_params method does not actually permit the fields in the create action. I've generated the relevant controller and views with rails generate devise:controllers user and rails generate devise:views user, and made modifications to both. In the view:
<h2>Custom User Sign up</h2>
<%= form_for(resource, as: resource_name, url: registration_path(resource_name)) do |f| %>
<%= devise_error_messages! %>
<%= fields_for(:extra_user_attributes) do |pa| %>
<div class="field">
<%= pa.label :first_name %><br />
<%= pa.text_field :first_name %>
</div>
<div class="field">
<%= pa.label :last_name %><br />
<%= pa.text_field :last_name %>
</div>
<% end %>
<div class="field">
<%= f.label :email %><br />
<%= f.email_field :email %>
</div>
<div class="field">
<%= f.label :password %>
<% if #minimum_password_length %>
<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 "user/shared/links" %>
We have a nested :extra_user_attributes field that we need permitted, so in the devise generated controller, I uncommented a couple of lines and added the relevant logic:
class CustomUser::RegistrationsController < Devise::RegistrationsController
before_action :configure_sign_up_params, only: [:create]
# POST /resource
def create
ap sign_up_params
end
protected
# If you have extra params to permit, append them to the sanitizer.
def configure_sign_up_params
devise_parameter_sanitizer.permit(:sign_up, keys: [:extra_user_attributes => [:first_name, :last_name]])
end
end
This is where I got stuck. I've tried several permutations in the keys array, but everything I've tried thus far yields the same result. For my own sanity, I verified the before_action actually does trigger.
As for the endpoint, I'm simply printing the output to the console just to see what the permitted signup parameters are.
Parameters: {"utf8"=>"✓", "authenticity_token"=>"5MqzDUsbrdMdE0Z7/Vw70zJddaKR+0eLYdnum3wEyTShNjB9o3Nb4ZsnE0dZEPlgs4SheZtQad0VpuIGtCeSmw==", "extra_user_attributes"=>{"first_name"=>"", "last_name"=>""}, "user"=>{"email"=>"", "password"=>"[FILTERED]", "password_confirmation"=>"[FILTERED]"}, "commit"=>"Sign up"}
{
"email" => "",
"password" => "",
"password_confirmation" => ""
}
Am I missing something here?
Workaround
A workaround would be to:
def other_signup_params
params.require(:extra_user_attributes).permit(:first_name, :last_name)
end
Which works just fine, but it just doesn't feel right. I feel like I should be able to use the built in configure_sign_up_parameters method that Devise provides but I'm somehow doing something wrong. Appreciate the help in advance.
The problem was not with devise, but a problem with the nested parameters I set up.
<%= fields_for(:extra_user_attributes) do |pa| %>
<div class="field">
<%= pa.label :first_name %><br />
<%= pa.text_field :first_name %>
</div>
<div class="field">
<%= pa.label :last_name %><br />
<%= pa.text_field :last_name %>
</div>
<% end %>
The fields_for did not get associated with the parent form, and got orphaned. It should have read:
<%= f.fields_for(:extra_user_attributes) do |pa| %>
Then, the sanitizer properly permits the fields:
{
"email" => "foo#example.com",
"password" => "hello!",
"password_confirmation" => "world!",
"extra_user_attributes" => {
"first_name" => "Foo",
"last_name" => "Bar"
}
}
I've added the resource helper methods to my settings controller so that I can update the current_user from within the controller. I've taken the form_for from registrations/edit and copied it into my settings controller view. I'm able to successfully update the user, but the existing values aren't showing up in the form as they would if I open users/registrations/edit. It's the same code displaying each form. What am I missing?
settings_controller.rb
helper_method :resource_name, :resource, :devise_mapping, :resource_class
def resource_name
:user
end
def resource
#resource ||= User.new
end
def devise_mapping
#devise_mapping ||= Devise.mappings[:user]
end
def resource_class
User
end
devise/registrations/edit.html.erb and general.html.erb
<%= form_for(resource, as: resource_name, url: registration_path(resource_name), html: { method: :put }) do |f| %>
<%= devise_error_messages! %>
<div class="field">
<%= f.label :email %><br />
<%= f.email_field :email, autofocus: true %>
</div>
<% if devise_mapping.confirmable? && resource.pending_reconfirmation? %>
<div>Currently waiting confirmation for: <%= resource.unconfirmed_email %></div>
<% end %>
<div class="field">
<%= f.label :password %> <i>(leave blank if you don't want to change it)</i><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="field">
<%= f.label :current_password %> <i>(we need your current password to confirm your changes)</i><br />
<%= f.password_field :current_password, autocomplete: "off" %>
</div>
<div class="actions">
<%= f.submit "Update" %>
</div>
<% end %>
I am trying to make a custom form login using devise.
This is the code I have
<h2>login</h2>
<%= form_for(:user, :url => user_session_path) do |f| %>
<div class="field">
<%= f.label :email %><br />
<%= f.email_field :email, autofocus: true %>
</div>
<div class="field">
<%= f.label :password %>
<% if #minimum_password_length %>
<em>(<%= #minimum_password_length %> characters minimum)</em>
<% end %><br />
<%= f.password_field :password, autocomplete: "off" %>
</div>
<div class="actions">
<%= f.submit "Login" %>
</div>
<% end %>
<%= render "devise/shared/links" %>
I generated the user model with the help of devise.
I am getting the following error
undefined local variable or method `resource_name' for #<#<Class:0x007f8e54d29468>:0x007f8e49dd7500>
<%= link_to "Log in", new_session_path(resource_name) %><br />
This is the source I followed
Getting devise sign_in form into Twitter Bootstrap Modal
Refer:
https://github.com/plataformatec/devise/wiki/How-To:-Display-a-custom-sign_in-form-anywhere-in-your-app
If you have set up the followings in your application_helper.rb -
helper_method :resource_name, :resource, :devise_mapping
def resource_name
:user
end
def resource
#resource ||= User.new
end
def devise_mapping
#devise_mapping ||= Devise.mappings[:user]
end
Then you should be able to do the following -
form_for(resource, :as => resource_name)
I want to create a new user from my form in my new.html.erb file:
<%= form_for(#user) do |f| %>
<div class="field">
<%= f.label :name %><br/>
<%= f.text_field :name %>
</div>
<div class="field">
<%= f.label :email %><br/>
<%= f.text_field :email %>
</div>
<div class="field">
<%= f.label :password %><br/>
<%= f.password_field :password%>
</div>
<div class="field">
<%= f.label :password_confirmation, "Confirmation" %><br/>
<%= f.password_field :password_confirmation %>
</div>
<div class="actions">
<%= f.submit "Sign Up"%>
</div>
<% end %>
In my users_controller.rb I have this
class UsersController < ApplicationController
def new
#title = "Sign Up"
#user = Users.new
end
And I have this in my routes.rb
resources :users
I keep getting an error: http://d.pr/wJm8 when I go to users/new and I can't figure out what's wrong...help please?
I think there are some plurality inconsistencies. Your model should be named User not Users.