I added an extra field in devise when a user signs up: "barge_name", when i go to the sign up page I enter a barge_name, email address and password this all works.
But when i try rails c, User.first is says barge_name is nil, I don't know why the value doesn't get picked up when signing up? Can someone help me with this?
#<User id: 1, email: "barcelona@gmail.com", created_at: "2019-10-31 17:43:21", updated_at: "2019-10-31 17:43:21", barge_name: nil>]
schema
create_table "users", force: :cascade do |t|
t.string "email", default: "", null: false
t.string "encrypted_password", default: "", null: false
t.string "reset_password_token"
t.datetime "reset_password_sent_at"
t.datetime "remember_created_at"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.string "barge_name"
t.index ["email"], name: "index_users_on_email", unique: true
t.index ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true
end
migration files
class AddDetailsToUsers < ActiveRecord::Migration[5.2]
def change
add_column :users, :barge_name, :string
end
end
class DeviseCreateUsers < ActiveRecord::Migration[5.2]
def change
create_table :users do |t|
## Database authenticatable
t.string :email, null: false, default: ""
t.string :encrypted_password, null: false, default: ""
## Recoverable
t.string :reset_password_token
t.datetime :reset_password_sent_at
## Rememberable
t.datetime :remember_created_at
add_index :users, :email, unique: true
add_index :users, :reset_password_token, unique: true
end
end
form
<h2>Sign up</h2>
<%= simple_form_for(resource, as: resource_name, url: registration_path(resource_name)) do |f| %>
<%= f.error_notification %>
<div class="form-inputs">
<%= f.input :barge_name,
required: true,
autofocus: true,
input_html: { autocomplete: "bargename" }%>
<%= f.input :email,
required: true,
autofocus: true,
input_html: { autocomplete: "email" }%>
<%= f.input :password,
required: true,
hint: ("#{@minimum_password_length} characters minimum" if @minimum_password_length),
input_html: { autocomplete: "new-password" } %>
<%= f.input :password_confirmation,
required: true,
input_html: { autocomplete: "new-password" } %>
</div>
<div class="form-actions">
<%= f.button :submit, "Sign up" %>
</div>
<% end %>
<%= render "devise/shared/links" %>
controller
class PositionsController < ApplicationController
def new
@position = Position.new
end
def index
@positions = Position.all
end
def show
@position = Position.find(params[:id])
end
def create
@position = Position.new(position_params)
if @position.save!
redirect_to @position
PositionMailer.general_message(@position).deliver
else
render :new
end
end
private def position_params
params.require(:position).permit(:date, :time, :activity, :tripnumber)
end
end
Started POST "/users" for ::1 at 2019-11-01 18:15:31 +0100
Processing by Devise::RegistrationsController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"K1WdKPpHZKESmfr7tZqi7yj2qM1SMIZWpXconGMbaZMZYrU1VRrpD6plMu2i79m6tWWYdAHQM98sPJzv2bDi4w==", "user"=>{"barge_name"=>"test", "email"=>"test@gmail.com", "password"=>"[FILTERED]", "password_confirmation"=>"[FILTERED]"}, "commit"=>"Sign up"}
Unpermitted parameter: :barge_name
You have to add the new param like this.
application_controller.rb
before_action :configure_permitted_parameters, if: :devise_controller?
...
protected
def configure_permitted_parameters
devise_parameter_sanitizer.permit(:sign_up, keys: [:barge_name])
devise_parameter_sanitizer.permit(:account_update, keys: [:barge_name])
end
Take notice of the two lines, one is for sign_up
and one is for account_update
, so new
and edit
actions basically.