向用户添加了详细信息,但未获取值

Added a detail to Users, but value doesn't get picked up

当用户注册时,我在设计中添加了一个额外的字段:"barge_name",当我进入注册页面时,我输入了 barge_name,电子邮件地址和密码,这一切都有效。

但是当我尝试 rails c 时,User.first 说 barge_name 为 nil,我不知道为什么在注册时没有提取该值?有人可以帮我解决这个问题吗?

 #<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>]

架构

  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

迁移文件

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

表格

<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" %>

控制器

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

您必须像这样添加新参数。

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

注意这两行,一是sign_up,一是account_update,所以基本都是newedit动作。