I tried both ways and came to the realization that you don't need the permit params. You only need to do 4 things:
- Add a name column in the migration
- Create app/controllers/users_contoller.rb to override gem controller
- Create the new form to accept name app/views/users/new.html.erb
- Modify your routes
Starting from the generated migration rails generate clearance:install
(before running rake db:migrate
)
add a name:string
column with an index such so that your migration looks like:
by adding this column, it is included in the creation of the user params.
def change
change_table :users do |t|
t.timestamps null: false
t.string :email, null: false
t.string :name, null: false, limit: 50
t.string :encrypted_password, limit: 128, null: false
t.string :confirmation_token, limit: 128
t.string :remember_token, limit: 128, null: false
end
add_index :users, :name
add_index :users, :email
add_index :users, :remember_token
end
Then you only need to create 2 files:
app/controllers/users_controller.rb
class UsersController < Clearance::UsersController
def create
@user = user_from_params
if @user.save
sign_in @user
redirect_to '/'
else
render template: 'users/new'
end
end
private
def user_from_params
user_params = params[:user] || Hash.new
name = user_params.delete(:name)
email = user_params.delete(:email)
password = user_params.delete(:password)
Clearance.configuration.user_model.new(user_params).tap do |user|
user.name = name
user.email = email
user.password = password
end
end
end
and
app/views/users/new.html.erb
<div id='clearance' class='sign-up'>
<h2><%= t('.title') %></h2>
<%= form_for @user do |form| %>
<div class='text-field'>
<%= form.label :name %>
<%= form.text_field :name, :type => 'name' %>
</div>
<div class='text-field'>
<%= form.label :email %>
<%= form.text_field :email, :type => 'email' %>
</div>
<div class='password-field'>
<%= form.label :password %>
<%= form.password_field :password %>
</div>
<div class='submit-field'>
<%= form.submit %>
</div>
<div class='other-links'>
<%= link_to t('.sign_in'), sign_in_path %>
</div>
<% end %>
</div>
Then your routes should include this
resources :users,
controller: 'users',
only: 'create'