-1

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
skosman
  • 27
  • 5
  • Can you post the form and controller you're hitting please – Mark Oct 31 '19 at 18:55
  • added the form and controller – skosman Oct 31 '19 at 18:59
  • this is a sign up form issue, not an issue in your `PositionsController `. When you sign up, look for something like "Unpermitted param barge_name", and you'll also see the controller that handles that request. You basically need to permit that new field in said controller action. The controller might be something like `RegistrationsController` under a Devise namespace, or a custom one in case you added it. – fanta Oct 31 '19 at 19:53
  • when i sign up i dont get any message it does work and i can sign up but only the value barge name is nil, so i can't use it anywhere .. I created my app with rails devise i don't see any controller which is linked to the sign up page – skosman Nov 01 '19 at 06:56

1 Answers1

1

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.

Rockwell Rice
  • 3,376
  • 5
  • 33
  • 61
  • I added this to my application controller but barge_name is still nill... after signing up with a new account – skosman Nov 01 '19 at 06:54
  • can you post an example of the params getting sent in your question? Keep these lines in, you will need them, but something else is off with the form. – Rockwell Rice Nov 01 '19 at 13:38
  • i am not sure what you mean with an example param? so when i sign up the value barge_name doesn't get up and with my fake data in the seed file, barge_name is not picked up either, can it be related? – skosman Nov 01 '19 at 16:53
  • So when you sign up, in your rails console you can see the parameters getting sent to the controller that looks something like this (but with your values) `:user => { :name => “test”, :email => “test@example.com”, :barge_name => ""} `. – Rockwell Rice Nov 01 '19 at 16:57
  • aah oke i see now! i added it in the first post, its is linked to registration controller but i can't find that in my app ? and i see it says unpermitted parameter barge_name – skosman Nov 01 '19 at 17:20
  • oh sorry, I forgot one line that also needs to be added into the application controller to call this, totally my bad. I updated my answer, `before_action :configure_permitted_parameters, if: :devise_controller?` needs to go at the top of the file, it should work now. – Rockwell Rice Nov 01 '19 at 17:24
  • Cool it works! thanks a lot man! so for every parameter I add I have to add them to the application controller – skosman Nov 01 '19 at 17:31
  • Well, you have to add it to the array in the permitted parameters. I place this in the application controller, and I think that is pretty common. – Rockwell Rice Nov 01 '19 at 18:15